Answer the question
In order to leave comments, you need to log in
How to pass data received through Mongoose to the template?
Good afternoon. If I get data from the database and display it on the page like this, then everything is output normally:
var selectedUser = User.findOne({'_id': '5991bf82b9a87a624407907e'}, (err, user) => {
console.log('result', err, user);
res.render("userpage.hbs", {
username: user.username
});
});
var selectedUser = User.findOne({'_id': '5991bf82b9a87a624407907e'}, (err, user) => {
console.log('result', err, user);
});
res.render("userpage.hbs", {
username: user.username
});
Answer the question
In order to leave comments, you need to log in
var selectedUser = User.findOne({'_id': '5991bf82b9a87a624407907e'}, (err, user) => {
console.log('result', err, user);
someFunction(user);
});
function someFunction (user) {
res.render("userpage.hbs", {
username: user.username
});
}
The 'user' variable is in a different scope, so the second option will not work in this form.
The easiest fix is to create a variable that is global to these two function calls and write everything you need into it. And then read the value in the render function
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question