Reputation: 604
I have this code
exports.index = function (req, res) {
res.render('product/index.jade', {
products: db.products.find().toArray(function (err, prods) {
if (!err)
return prods;
return [];
})
});
};
What I'm trying to do here is pass the query result to a jade view which expects a parameter named "products". I ve tried a lot of combinations but neither of them works (right now i'm getting "cannot read proprty lenght...."). I know it must be a rookie mistake, but i cannot figure it out, any help will be apreciated!
PS: I'm using express and mongodb. And yes, the products collection does contain products.
Upvotes: 0
Views: 523
Reputation: 2022
toArray doesn't actually return a value. Instead, the callback function gets called with the products as a parameter once the lookup is done. this is due to node's async I/O nature.
Your code should probably look something like this
exports.index = function (req, res) {
db.products.find().toArray(function (err, prods) {
if (!err)
res.render('product/index.jade', {products: prods});
else
res.render('product/index.jade', {products: []});
});
});
Upvotes: 1