【发布时间】:2016-12-04 19:05:12
【问题描述】:
我正在尝试使用 NodeJS 中的 mongoose 在我的 MongoDB 数据库中生成一个 Data 对象。
这是sn-p的代码:
console.log(RecipeData.ingredients);
RecipeData.spices.forEach(function(spice){
module.exports.findSpiceByName(spice, function(err, res){
if (err){
return callback(err);
}
RecipeData.ingredients.push(res._id);
return callback(null, RecipeData);
});
});
console.log(RecipeData.ingredients);
基本上,我的 RecipeData 是一个具有一些属性的对象。在这种情况下,主要是香料和配料。这两个都是字符串列表。
- 我想检查 RecipeData.spice:
- 如果列表中有一个元素:(该元素将是香料的名称)
- 我想在我的 Spices Collection 中找到与该名称关联的香料,并将其 _id 永久添加到我的 RecipeData.ingredients 中
- 如果列表中有一个元素:(该元素将是香料的名称)
在我用来测试此代码的示例中,console.log 的输出不应该相同。
知道为什么变量 RecipeData.ingredients 在 ForEach 循环之外没有改变吗?
提前感谢您的帮助。
【问题讨论】:
标签: javascript arrays node.js object mongoose