this 的节点异步库绑定
我正在使用异步库(https://github.com/caolan/async) 在尝试与 mongoskin 异步执行多个数据库查询的节点上(https://github.com/guileen/node-mongoskin )
问题是,当使用像这样的映射函数时
app.post '/events', (req, res) ->
storage.events.getByUser req.session.authId, (events) ->
async.map events, storage.codes.getCountByEvent, (err, results) ->
res.send results
,它会将 @
绑定到 getCountByEvent
函数上的全局命名空间,任何有异步库经验的人都会这样做吗?能够给予我有关解决此问题的最佳方法的指导?
的 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
以下是在 async.map
之外调用 getCountByEvent
它可以正常工作,提前致谢
I am using the async library (https://github.com/caolan/async)
on node trying to execute multiple db queries async with mongoskin (https://github.com/guileen/node-mongoskin)
The problem is when using the map function like so
app.post '/events', (req, res) ->
storage.events.getByUser req.session.authId, (events) ->
async.map events, storage.codes.getCountByEvent, (err, results) ->
res.send results
it is binding @
to the global namespace on the getCountByEvent
function, does anybody with experience with the async library able to give me guidance on the best way to fix this?
Here is the sample of the storage.codes
implementation
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
calling getCountByEvent
outside of the async.map
it will work fine
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建绑定到
storage.codes
对象的getCountByEvent
版本:You could create a version of
getCountByEvent
that's bound to thestorage.codes
object: