gpt4 book ai didi

node.js - this 的 Node 异步库绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 23:46:45 26 4
gpt4 key购买 nike

我正在使用异步库( https://github.com/caolan/async )在尝试与 mongoskin 异步执行多个数据库查询的 Node 上 ( https://github.com/guileen/node-mongoskin )

问题是像这样使用 map 函数时

app.post '/events', (req, res) ->
storage.events.getByUser req.session.authId, (events) ->
async.map events, storage.codes.getCountByEvent, (err, results) ->
res.send results

它正在将 @ 绑定(bind)到 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它会正常工作

提前致谢

最佳答案

您可以创建绑定(bind)到 storage.codes 对象的 getCountByEvent 版本:

async.map events, storage.codes.getCountByEvent.bind(storage.codes), ...

关于node.js - this 的 Node 异步库绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9334799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com