我实际上用mongodb和nodejs开发了一个小应用程序来创建我的REST Api。 当我需要访问对象引用时遇到问题:
我找到了使用Wait.for库的解决方案:https://github.com/luciotato/waitfor,但我不知道它是如何工作的。我尝试了一切,但没办法让它发挥作用
all: (req,res)->
@em.collection(@collection).find().toArray((err, result)=>
roadmaps = []
for r in result
r.user = @getUser(r.user.oid)
roadmaps.push r
res.send(roadmaps))
getUser: (oid)->
@em.collection('user').findOne {_id: new @objectId(oid)}, (err, res)=>
if !err
return res
return undefined
有人知道如何让它正常工作吗?我应该把wait.lauchFiber放在哪里?我应该把wait.for放在哪里?
全部谢谢
答案 0 :(得分:0)
我不熟悉CoffeeScript,请纠正我,我会编辑这个答案。
all: (req,res)->
var result = wait.forMethod(@em.collection(@collection).find(), "toArray")
roadmaps = []
for r in result
r.user = @getUser(r.user.oid)
roadmaps.push r
res.send(roadmaps)
getUser: (oid)->
try
return wait.forMethod(@em.collection('user'),"findOne",{_id:new @objectId(oid)})
catch(err)
return undefined
正如您所看到的,对于“getUser”,如果方法很简单,那么最好使用您的版本和回调。
您在请求到达时放入launchFiber。见https://github.com/luciotato/waitfor#proper-use