从mongoose中查询数据并将其显示在html页面上
这是我的模式
var productSchema = new mongoose.Schema({ name: String, description: String }); var Product = mongoose.model('Product', productSchema);
在我使用的index.js中
exports.welcome = function(req,res) { Product.find({},{},function(err,docs) { res.render('welcome', { "productlist" : docs }); }); };
在我的app.js中,我打电话给这个语句,其中路由是我的variables在index.js app.get('/ welcome',routes.welcome)中调用welcome。
我的模式也写在index.js。 我想要做的是在名为“welcome.html”的html页面中显示所有产品的名称和说明。
任何人都可以告诉我喜欢什么,我应该写在我的HTML页面来做到这一点。
从你最新的评论,这意味着你正在使用EmbeddedJS作为模板引擎。 你的答案在这里有很好的文档。
对于共谋,一个welcome.html
来显示结果的例子是:
<!DOCTYPE html> <html> <head> <title>My Products</title> </head> <body> <ul> <% for(var i=0; i<productlist.length; i++) {%> <li><%= productlist[i].name %> : <%= productlist[i].description %></li> <% } %> </ul> </body> </html>