0

I have a series of models:

    returns = {}
    cnt = 0
    data_models = [Item, Product, Specs] # these are mongoose Models, previously defined

    for d in data_models
      console.log "Getting objects for " +d.modelName
      d.find((err, objects) ->
        console.log "Getting results for " d.modelName
        name = d.modelName
        returns.name = objects
        cnt += 1
        if cnt is data_models.length
          console.log "All data retrieved"
          res.send(returns)

In the first log, I correctly get

Getting objects for Item
Getting objects for Product
Getting objects for Specs

but inside the callback, I get

Getting results for Specs
Getting results for Specs
Getting results for Specs

So it seems the reference over the callback is not kept during the call?

What I am trying to do is to get the (filtered) objects of all three elements, and only when I have them all, I return an object to the client.

transient_loop
  • 5,984
  • 15
  • 58
  • 117
  • 1
    Or these CoffeeScript questions: http://stackoverflow.com/q/11996218/479863, http://stackoverflow.com/q/17332141/479863 – mu is too short Nov 29 '13 at 19:44
  • I loved the forEach loop! That saved my day. Thanks muistooshort. I was trying @Pointy 's solutions, but honestly I was struggling, because I didn't want to use global variables. I guess the forEach is only available to coffee, not javascript? – transient_loop Nov 29 '13 at 20:07
  • 1
    forEach is available in js too – Diadara Nov 29 '13 at 20:47
  • `forEach` is a method that newer Javascripts provide for arrays. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – hpaulj Dec 01 '13 at 05:31

0 Answers0