Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query data from mongoose and display it on a html page

This is my schema

var productSchema = new mongoose.Schema({
    name: String,
    description: String
});
var Product = mongoose.model('Product', productSchema);

In my index.js i am using

exports.welcome = function(req,res) {
    Product.find({},{},function(err,docs) {
        res.render('welcome', {
            "productlist" : docs
        });
    });
};

In my app.js i am calling this statement where routes is my variable to call welcome in index.js app.get('/welcome',routes.welcome);

My schema is also written in index.js. What i want to do is display all the products with their name and description in my html page named "welcome.html".

Can anyone tell me like what should i write in my html page to do this.

like image 583
user3789868 Avatar asked Feb 02 '26 13:02

user3789868


1 Answers

From your latest comment, it means you are using EmbeddedJS as templating engine. Your answer is well documented here.

For complicity, an example of welcome.html to display results is:

<!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>
like image 121
alandarev Avatar answered Feb 05 '26 06:02

alandarev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!