【发布时间】:2015-06-05 19:12:55
【问题描述】:
我想在我的 mongoDB 中保存到两个集合中。这个操作是异步的,所以我在咖啡中使用 for 和 do。
for machine in machines
do(machine) ->
//if machine does not exist
for part in machine.parts
do(part) ->
//if not part not exists --> save
//push part to machine parts list
//save machine
机器零件稍后在数据库中为空。如何让第一个 do 循环等待第二个 do 循环完成?
编辑真实代码示例:
recipeModel = require('../models/recipe.model')
ingredientModel = require('../models/ingredient.model')
#Save Recipe into the database
async.map recipes, (recipe, next) ->
recipeDBObject = new recipeModel()
recipeDBObject.href = recipe.href
recipeDBObject.ingredients = []
recipeModel.find({ href: recipe.href }, (err, recipeFound) ->
return next err if err
return next null, recipeFound if recipeFound.length > 0
recipeDBObject.title = recipe.title
ingredientsPushArray = []
console.log recipe.href
async.map recipe.zutaten, (ingredient, cb) ->
#Save all ingredients
ingredient.idName = ingredient.name.replace(/[^a-zA-Z0-9]+/gi, "").toLowerCase()
ingredientModel.find({ idName: ingredient.idName }, (err, ingredientFound) ->
return next err if err
if ingredientFound.length >0
ingredientDBObject = ingredientFound[0]
else
ingredientDBObject = new ingredientModel()
ingredientDBObject.name = ingredient.name
ingredientDBObject.save()
recipeDBObject.ingredients.push({"idName":ingredient.idName, "name":ingredient.name, "amount":ingredient.amount})
return cb(null, true)
)
recipeDBObject.ingredients = ingredientsPushArray
recipeDBObject.save()
return next(null, true)
)
我仍然无法正常工作。保存食谱,节点构建成分数组,但它既不保存成分也不将数组保存到食谱中。
编辑 2:
async.map recipes,
(recipe, next) ->
recipeDBObject = new recipeModel()
recipeDBObject.href = recipe.href
recipeDBObject.ingredients = []
recipeModel.find({ href: recipe.href }, (err, recipeFound) ->
return next err if err
return next null, recipeFound if recipeFound.length > 0
recipeDBObject.title = recipe.title
ingredientsPushArray = []
ingredientsArray = []
async.map recipe.zutaten,
(ingredient, cb) ->
#Save all ingredients
ingredient.idName = ingredient.name.replace(/[^a-zA-Z0-9]+/gi, "").toLowerCase()
ingredientModel.find({ idName: ingredient.idName }, (err, ingredientFound) ->
return next err if err
ingredientsArray.push({"idName":ingredient.idName, "name":ingredient.name, "amount":ingredient.amount})
if ingredientFound.length >0
return cb(null, true)
else
ingredientDBObject = new ingredientModel()
ingredientDBObject.name = ingredient.name
ingredientDBObject.idName = ingredient.idName
ingredientDBObject.save((err) ->
#console.log "some erros because required is empty" if err
return cb err if err
#console.log "ingredient saved"
return cb(null, true)
)
(err, ingredientsArray) ->
console.log "This is never logged"
return err if err
recipeDBObject.ingredients = ingredientsArray
recipeDBObject.save((err)->
return err if err
return next(null, true)
)
)
)
(err) ->
console.log "show me the errors: ", err if err
现在成分已保存,但食谱未保存。
有趣的资源: http://www.hacksparrow.com/managing-nested-asynchronous-callbacks-in-node-js.html
【问题讨论】:
-
您不是在等待
.save()方法完成,也不是在处理来自async.map的回调。由于您实际上并未处理异步内容,因此您的示例的工作方式与同步 for 循环非常相似。 -
我的意思是,你只是调用
obj.save(),然后同步调用asnc回调,而不是像obj.save next那样将它传递给猫鼬。 -
由于您没有处理
async.map回调,因此您尝试在处理任何recipe.zutaten对象之前保存recipeDBObject。您应该将这部分代码移至async.map回调。 -
您没有正确处理
async错误,例如您在(ingredient, cb) ->迭代器函数中调用return next err if err而不是return cb err if err,并返回错误return err if err而不是将其传递给下一个处理程序return next err if err。不过,我不明白为什么在您的示例中调用console.log较新(除非在猫鼬调用中发生实际错误)。 -
将数据推送到共享数组并不是将其传递给最终回调的好方法。由于您使用的是
async.map而不是async.forEach,因此您可以简单地将成分传递给cb回调(作为它的第二个参数)。否则,ingredientsArray数组中的元素顺序将与原始数组中的不同。好吧,无论如何,您正在将共享的ingredientsArray变量与本地变量进行映射。
标签: coffeescript