I'm using node/express/mongoose/mongodb, and I have a collection of "packets." the users can individually set the status of the "packets," and I trying to show them the list of all the packets, and show them the status that they set for the packet. I've been working on this for a few days and can't make it work right.
What I think has to happen is that I have to query the first list, and then loop through an array of the results and add the results, if there is results to the first packet.
It seems to be either a closure problem or a problem with just the timing to return results. No matter what I've figured out to try works.
Thanks,
Here's where I am:
app.get('/api/packets/:_id', stormpath.groupsRequired(['Paid']),
stormpath.getUser, function(req, res, next){
var status;
var pack;
var newStatus;
try{
Packet.find({courseId: req.params._id})
.exec( function(err, packets){
pack = packets;
for (i = 0; i < pack.length; i++){
var promise = History.findOne({
packetId: pack[i]._id,
userId: req.user.email
})
.select('status')
.exec( function(err, package){
if(package == null){
status = 1
} else {
status = package.status;
};
newStatus = status
});
promise.then(pack[i].status = newStatus);
};
res.json(pack)}
});
}catch(err){
next(err);
};
});
via noob2017
No comments:
Post a Comment