节点asynchronous库绑定这个
我正在使用asynchronous库( https://github.com/caolan/async )在节点上尝试执行与mongoskin( https://github.com/guileen/node-mongoskin )asynchronous的多个db查询
问题是使用像这样的地图function
app.post '/events', (req, res) -> storage.events.getByUser req.session.authId, (events) -> async.map events, storage.codes.getCountByEvent, (err, results) -> res.send results
它将@
绑定到getCountByEvent
函数上的全局名称空间,是否有任何具有asynchronous库经验的人能够给我指导最好的方法来解决这个问题?
这是storage.codes
实现的示例
class Codes constructor: (db) -> db.bind 'codes', getCountByEvent: (event, callback) -> @.find(event: event._id).toArray (err, res) -> callback res.length return db.codes exports.Codes = Codes
调用getCountByEvent
之外的async.map
它会正常工作
提前致谢
您可以创build绑定到storage.codes
对象的getCountByEvent
版本:
async.map events, storage.codes.getCountByEvent.bind(storage.codes), ...