Passing A Variable From Routes.js To My .ejs Page
Solution 1:
The result from your database query is put in the rows part of your callback function. It is not returned by using the query itself. So, if you were to use it and pass it to your template, you can put them inside the callback function. Try this:
connection.query("SELECT about FROM users WHERE username = ?",
req.user, function(err, rows) {
res.render("profile", {user: req.user, about: rows[0]}) //Notice you don't need to add ejs to the end of the file
})
Solution 2:
The return value of connection.query
is not the result of the query. Rather it's a sequencing object that allows advanced use of the query. The result is the second argument passed to the callback you are providing. In your example it's named rows
. The reason for this is because the query is asynchronous. The callback is invoked when the query completes.
There's no perfect way of answering your question because doing this correctly depends on how your application is structured. However, as a temporary fix this should work:
var aboutUser = connection.query("SELECT about FROM users WHERE username = ?", req.user, function(err, rows) {
res.render('profile.ejs', {
user: req.user,
about: rows
});
});
You don't want to do this in the long run for a variety of reasons but at least this should get you started.
Post a Comment for "Passing A Variable From Routes.js To My .ejs Page"