I want to render a page with the code:
exports.new = function(req, res){
res.render('products/new', {
title: 'New Product',
product: new Product({}),
categories: Category.list()
})
}
Category is a Mongoose schema. If I try to get list of all categories, it works asynchronously.
How I can get the list of all categories from a Mongo DB using Mongoose?
As you already know, DB queries in Node are always asynchronous; so, you'll need to wait for the query to complete and then render your template in its callback!
In the end, it will be something like this:
Category.list(function( err, categories ) {
res.render("products/new", {
title: "New Product",
product: new Product({}),
categories: categories
});
});