我有一系列模特:
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)
在第一个日志中,我正确
Getting objects for Item
Getting objects for Product
Getting objects for Specs
但在回调中,我得到了
Getting results for Specs
Getting results for Specs
Getting results for Specs
所以在调用期间似乎没有保留对回调的引用?
我想要做的是获取所有三个元素的(过滤的)对象,并且只有当我拥有它们时,我才将对象返回给客户端。