Reputation: 33439
I am using mongoskin nodejs plugin to talk to mongodb. But the problem is all mongoskin API methods are async and I am using a synchronous nodejs server (using express) to serve the webpages. How do I accomplish something like this:
server.get('/woofs', function(req, res) {
var ret;
woofDb.find().toArray(function(err, i) {
//do something with each i to construct ret
});
res.end(ret);
});
Upvotes: 0
Views: 1031
Reputation: 23070
The answer is to not try to use a synchronous node.js server. It completely defeats the purpose of using node.js and the sooner you embrace that the more pleasant your node.js experience will be. That being said, the following code should get you pretty close to what you're trying to do.
server.get('/woofs', function(req, res) {
woofDb.find().toArray(function(err, i) {
var ret;
//do something with each i to construct ret
res.end(ret);
});
});
Upvotes: 2