1

I am working on a parse application using express. I have an index file which shows the same information to the logged in users and public users except a login to public users and logout to logged in users. I am using app.locals to store a flag and logged-in user name but some how they are not getting stored. My code for app.js is

// The homepage renders differently depending on whether user is logged in.
app.get('/', function(req, res) {
if(Parse.User.current()){

  Parse.User.current().fetch().then(function(user) {
     // Render the user profile information (e.g. email, phone, etc).
     name = user.get('username');
  });
  app.locals({flag :true,name:name});

}else{
  app.locals({flag:false, name:''});
}
// Render a public welcome page, with a link to the '/' endpoint.
  var ProfessionalUsers = Parse.Object.extend("User");
  var query = new Parse.Query(ProfessionalUsers);
  query.limit(50);
  query.equalTo("usertype", "Professional");
  query.find().then(function(profs) {
    res.render('index', {
      title: "Popular Experts",
      profs: profs,
    });
  });



});

The code I have written in index

<div class='navigation'>
    <ul class='navigation-ul'>
        <li><a href='/' class='search' id='searchLink' >Search</a></li>
        <% if(flag){ %>
        <li><a href='/dashboard' class='dashboard' id='dashboardLink' >Dashboard</a></li>
        <% } %>
        <li><a href='/howitworks' class='how-it-works' id='howItWorksLink'>How it Works</a></li>
        <li><a href='/testimonials' class='testimonials' id='testimonialsLink' >Testimonials</a></li>
        <li><a href='/faqs' class='faqs' id='faqsLink'>FAQs</a></li>
        <% if(flag){ %>
        <li><a href='/logout' class='logout' id='logoutLink'>Logout<span class='username'>(<%= name ? name : '' %>)</span></a></li>
        <% }else{ %>
        <li><a href='/login' class='login-signup' id='navLoginSignupLink'>Login/Signup</a></li>
        <% } %>


    </ul>

</div>

the code to show professionals

<p><%= title %></p>
 <div class='professionals'>

 <% for(var i=0;i<profs.length;i++) { %>

<div class='professional'>

    <div class='picture-space' onclick = 'location.href = "/user/<%= profs[i].id %>"' style='cursor:pointer'>
      <img src='images/default.jpg'/>
    </div>
      <div class='name-space'><%= profs[i].attributes.username %></div>
      <div class='service-space'><%= profs[i].attributes.servicetype %></div>
      <div class='linkedin-space'><a href='http://<%= profs[i].attributes.linkedin %>' target='_blank'>Linkedin Profile</a></div>


</div>
<% } %>
</div>

EDIT:

This is what I have done to fix this

// The homepage renders differently depending on whether user is logged in.
app.get('/', function(req, res) {

// Render a public welcome page, with a link to the '/' endpoint.
  var ProfessionalUsers = Parse.Object.extend("User");
  var query = new Parse.Query(ProfessionalUsers);
  query.limit(50);
  query.equalTo("usertype", "Professional");
  query.find().then(function(profs) {
    var user = Parse.User.current();
    if(user){
      user.fetch().then(function(user,error) {
        res.render('index',{user:user.toJSON(),profs:profs,title:'Popular Experts'});            
      });
    }else{
      res.render('index', {
      title: "Popular Experts",
      profs: profs,
      user:''
    });
    }


  });  

  });
spod
  • 406
  • 3
  • 5
  • 19

1 Answers1

1

app.locals doesn't have access to the req, res objects. It is intended for non-dynamic data that doesn't change per request (like app-name, or something similar). I believe what you are after is res.locals (http://expressjs.com/api.html#res.locals)

I would suggest adding some middleware that does what you want.

You could load it just for your route. This will execute the middleware RIGHT before it executes the function you defined at that route:

function getUserInfo(req, res, next) {
   var user_name = "adam"; //make some call here to get the user data

   //make 'user_name' variable available in the view
   res.locals.user_name = user_name;

   next(); //go to the next 'middleware', which is the stuff you defined.

}

//add getUserInfo here as middleware that will run on the '/' route BEFORE your middleware runs. Yours will be executed when 'next()' is called from the middleware function.
app.get('/', getUserInfo, function(req, res) {
Jamis Charles
  • 5,827
  • 8
  • 32
  • 42
  • That did not solve the problem. I have used a query inside a query to solve it. I feel that the solution is not right but I had no option. – spod Dec 05 '13 at 10:49
  • Can you show what you wound up doing? Perhaps we can get you to a better solution. – Jamis Charles Dec 05 '13 at 13:53