I am new to express and am pretty confused on how I should access data on the front-end side,
I have the following code
exports.init = function(req, res){
if (req.isAuthenticated()) {
//res.send();
var userId = req.user._id
res.render('account/usernotes');
}
else {
res.render('signup/index', {
oauthMessage: '',
oauthTwitter: !!req.app.config.oauth.twitter.key,
oauthFacebook: !!req.app.config.oauth.facebook.key,
oauthGoogle: !!req.app.config.oauth.google.key
});
}
};
I want to send UserId to the view so that I can access it so that I can fetch data using it, but so far using send(); renders the value of the data to the front-end, so how do I access it. I am guessing
res.render('account/usernotes',req.user._id);
should work but how do I access the data after this ?
When I use Jade/EJS templates, it is something like this:
res.render('account/usernotes', {userid: req.user._id});
In jade file:
p #{userid}
In ejs file you can do:
<%=userid%>
Here is the way to send data from server to client.
Here is your server side code.
res.render('index', { data1: 'Hello', data2: 'World' });
Get data on your view which is index.ejs.
<h1> <%= data1 + ' ' + data2 %> </h1>
For Further Reference See This
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With