I m actually developping a little application with mongodb and nodejs to create my REST Api. I face a problem when I need to access an object reference :
I have found a solution using Wait.for library : https://github.com/luciotato/waitfor , but I dont know how it works. I tried everything, but no way to make it work
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
Does anybody have an idea of how to make it works properly ? Where should I put the wait.lauchFiber ? where should I put the wait.for ?
Thanks for all
I'm not familiar with CoffeeScript, please correct me and I'll edit this answer.
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
As you can see, for "getUser", if the method is that simple, you better use your version, with the callback.
you put the launchFiber when a request arrives. see https://github.com/luciotato/waitfor#proper-use