coffeescript - Coffescript Nested Do Loop / Async Nested Loop -
i want save 2 collections in mongodb. operations async use , in coffee.
for machine in machines do(machine) -> //if machine not exist part in machine.parts do(part) -> //if not part not exists --> save //push part machine parts list //save machine
the machine parts empty later in db. how can make first loop wait second loop finish?
edit real code example:
recipemodel = require('../models/recipe.model') ingredientmodel = require('../models/ingredient.model') #save recipe 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 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) )
i still don't working. recipes saved, node builds ingredients array neither saves ingredients nor save array recipes.
edit 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 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 empty" if err return cb err if err #console.log "ingredient saved" return cb(null, true) ) (err, ingredientsarray) -> console.log "this 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 errors: ", err if err
now ingredients saved recipes aren't.
interesting ressources: http://www.hacksparrow.com/managing-nested-asynchronous-callbacks-in-node-js.html
the easiest way use module for managing asynchronous control flow, example
here simple examples.
using async.map
async = require 'async' async.map machines, (machine, next) -> # process single machine object machine.findbyid machine._id, (err, found) -> return next err if err # return error return next null, found if found # return object found async.map machine.parts, (part, cb) -> # save part db , call cb callback afterward part.create part, cb (err, parts) -> return next err if err # propagate error next handler # parts have been saved machine.parts = parts # save machine db , call next callback afterward machine.create machine, next (err, machines) -> if err # went wrong else # machine objects have been processed
using promises , when
module
when = require 'when' machines_to_save = when.filter machines, ({_id}) -> machine.findbyid(_id).then (found) -> not found when.map machines_to_save, (machine) -> when.map machine.parts, (part) -> part.create part .then (parts) -> machine.parts = parts machine.create machine .then (saved_machines) -> # machines saved .otherwice (err) -> # went wrong
Comments
Post a Comment