For example, I'm using a form to display all info from the DB:
<form action="/Userlist" method="get">
<input type="submit" value="Get All">
</form>
<P>
Should go here
</P>
This is served just fine by a get request:
app.get('/userlist' , function (req , res) {
User.find({}, 'comment').then(function (users) {
res.json(users);
});
});
But it is just a static page. How can I write the result from Mongoose to for example a HTML
tag - such that all comments are async displayed on the landing page?
welcome to stackOverflow. Please follow some good tutorials before asking here, tons out there. Just Google CRUD in node.js & mongodb. For your problem, you need to render the page instead of returning json.
res.render('index.ejs', {users})
instead of res.json(users);
& the html should be .ejs
This looks like a nice tutorial to start https://zellwk.com/blog/crud-express-mongodb/ Happy coding!
You can use AJAX for the get request somewhere inside you static html page in a script
tag.
Below is an example of how to do this.
$.get("http://localhost:8080/userlist", function(users){
var i;
for (i = 0; i < users.length; i++) {
$( "p" ).append( "<div>" + users[i] + "</div>" );
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.