0

route.js

router.get('/restful', function(req, res){
  console.log("before");
  User.show_deatils(req, res, function(err, resultArray){
   if(!err) {
     req.session.resultArray=resultArray;
    }
  });
  console.log(req.session.resultArray);
  res.render('restful',{resultArray:req.session.resultArray});
});

I don't know why I am getting as undefined when I am doing it console.log() in the above position.If I do console.log() just after the req.session.resultArray=resultArray then we are getting the result array.

I want to display this resultArray in my view.ejs. Can anyone suggest me how to solve it.What is the thing I am missing it out?

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
riza
  • 123
  • 2
  • 11
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Joe Clay Feb 28 '17 at 13:06

2 Answers2

0

The console.log runs before the async function actually gets the details from the database. so you get undefined. Do the render inside the callback. i.e, after you assign result array to session.

router.get('/restful', (req, res, next) {
      User.show_deatils(req, res, function(err, resultArray) {
          if(!err) {
              req.session.resultArray=resultArray;
              return res.render('restful' {resultArray:req.session.resultArray}));
           }
       });
});
Gnanesh
  • 668
  • 7
  • 13
  • This does not allow the page to load if I assign it after session – riza Feb 28 '17 at 13:13
  • did you try ?.What do you mean by after session. The basic idea here is, when you enter the page, server gets the details from the database and then, the `resultArray` would be assigned to `req.session` variable and only after that, the function actually returns by rendering the page with the resultArray. – Gnanesh Feb 28 '17 at 13:20
  • I dont know why This is not allowing to load the page. router.get('/restful', function(req, res){ console.log("before"); User.show_deatils(req, res, function(err, resultArray){ if(!err) { req.session.resultArray=resultArray; console.log(req.session.resultArray); } }); res.render('restful',{resultArray:req.session.resultArray}); }); This is allowing to navigate to the restful page.But the value of resultArray is becoming "undefined". Where I am doing the mistake?Cant able to find it out. – riza Mar 01 '17 at 05:39
  • When I used req.session.save().The data is been fetched now.But on the view page.The data is not been loaded at the first navigation.Wen we load again the page the data is been loaded.How to fix the issue.Anyone help me out. – riza Mar 01 '17 at 07:39
  • 1
    @riza place the render function just right after you do save() inside the callback. (User.show*) – Gnanesh Mar 01 '17 at 07:54
0

Just do like this

    router.get('/restful', function(req, res){
           console.log("before");
          User.show_deatils(req, res, function(err, resultArray){
           if(!err) { 

             res.render('restful',{resultArray:resultArray});
           }
          });
       });

I think this is what you want.

Prakash Sharma
  • 15,542
  • 6
  • 30
  • 37