【发布时间】:2019-11-10 01:02:33
【问题描述】:
我正在使用“request-promise”模块从某些 API 获取数据。
这将数组值作为主体。
我想将该数组保存为 mongoDB 中的每个文档。
因此,我用'for'作为循环。
但是当我用 console.log 检查这个时,
我希望像下面一样。
Current i is : 0
orderFind
Current i is : 1
orderFind
Current i is : 2
orderFind
Current i is : 3
orderFind
但它给了我
Current i is : 0
Current i is : 1
Current i is : 2
Current i is : 3
orderFind
orderFind
orderFind
orderFind
我尝试了异步,也尝试了等待。但是效果不好..
exports.saveOrder = (req, res) => {
rp({
method: "GET",
uri: "https://robot.com",
json: true
}).then(body => {
for (let i = 0; i < body.length; i += 1) {
console.log(`Current i is : ${i}`);
const eachBody = body[i];
Order.findOne(
{
order_id: eachBody.order_id
},
(err, exOrder) => {
console.log("orderFind");
if (err) {
return res.send(err);
}
}
);
}
});
};
【问题讨论】:
-
为什么要日志是这个命令?它不会改变 findOne 的结果
标签: node.js