Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Data to the front end in Express and accessing it

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 ?

like image 964
Bazinga777 Avatar asked Sep 18 '14 05:09

Bazinga777


2 Answers

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%>
like image 169
Anthony Avatar answered Nov 19 '22 23:11

Anthony


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

like image 25
Waqas Ahmed Avatar answered Nov 19 '22 22:11

Waqas Ahmed