0

I want to save into two collections in my mongoDB. This operations are async so I use for and do in coffee.

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

The machine parts are empty later in the db. How can I make the first do loop wait for the second do loop to finish?

EDIT Real Code Example:

  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)
    )

I still don't get it working. Recipes are saved, node builds the ingredients array but it neither saves the ingredients nor does it save the array into the 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 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

Now the ingredients are saved but the recipes aren't.

Interesting ressources: http://www.hacksparrow.com/managing-nested-asynchronous-callbacks-in-node-js.html

5
  • You're not waiting for .save() methods to complete and not handling callbacks from async.map. Since you're not actually handling your async stuff, your example works pretty much like synchronous for loop did. Apr 1, 2015 at 10:31
  • I mean, you just calling obj.save() and then synchronously invoking asnc callback, instead of passing it to mongoose like obj.save next. Apr 1, 2015 at 10:36
  • And since you're not handling async.map callback, you're trying to save recipeDBObject before any of recipe.zutaten objects have been processed. You should move this part of your code to the async.map callback. Apr 1, 2015 at 10:38
  • You're not handling async errors correctly, e.g. you're calling return next err if err inside of (ingredient, cb) -> iterator function instead of return cb err if err, and returning an error return err if err instead of passing it to the next handler return next err if err. Though, I can't see why console.log is newer called in your example (unless an actual error occurred in mongoose call). Apr 1, 2015 at 12:10
  • Pushing data to a shared array is not a very good way to pass it to the final callback. Since you're using async.map and not async.forEach, you could simply pass ingredients to the cb callback (as its second argument). Otherwise, the order of elements in ingredientsArray array will not be the same as it was in original array. Well, and you're shadowing shared ingredientsArray variable with a local one, anyway. Apr 1, 2015 at 12:14

1 Answer 1

2

The easiest way is to use some module for for managing asynchronous control flow, for example

Here are some 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 immediately
      return next null, found if found # return the object we found
      async.map machine.parts,
        (part, cb) ->
          # Save part to DB and call cb callback afterward
          Part.create part, cb
        (err, parts) ->
          return next err if err # propagate error to the next handler
          # All parts have been saved successfully
          machine.parts = parts
          # Save machine to DB and call next callback afterward
          Machine.create machine, next
  (err, machines) ->
    if err
      # Something went wrong
    else
      # All machine objects have been processed successfully

Using promises and 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) ->
  # All machines are saved
.otherwice (err) ->
  # Something went wrong
13
  • Thanks for your answer. I guess this will be the solution but I still don't get it running, could you take a brief look at the code above.
    – Andi Giga
    Apr 1, 2015 at 10:22
  • @AndiGiga I changed code formatting in my async example to make it easier for for you to understand what's going on there. Apr 1, 2015 at 10:44
  • @AndiGiga also added a lot of comments to explain each step for you. Apr 1, 2015 at 10:50
  • That is what I think I understood. #1) If I call a second function after a first function, like you did with (part, cb) -> & (err, parts) ->), then the first function is executed on every item of the Array, then the second function gets executed? #2) If there is a return in the first function, the second function still gets executed? #3) You can pass params between the first and the second function by just declaring them in the second functions arguments? #4) The async.map only continues with the next element when the callback is resolved. E.g. what you do with return next err if err?
    – Andi Giga
    Apr 1, 2015 at 11:36
  • 1
    Actually I noticed I have to use async.mapSeries otherwise he does not wait for the callback which causes duplicate entry errors.
    – Andi Giga
    Apr 2, 2015 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.