node.js - How to assign query results to an object -


i trying transfer results data query function object. console.log(results) line returns 'undefined' result. should do?

module.exports = {      show: function(req, res) {          var results;         user.native(function(err, user) {              if(err) {                                     console.log("there no exist user _id");             }              user.findone({'_id' : req.param('id')},                      function(err, user) {                       results = user;             });          });          console.log(results);         return res.view({ stuff : results });     } }; 

you have async issue, callback findone isn't executed in line rest of code, console.log(results) before results = user gets called. you'd want change this:

show: function(req, res) {      var results;     user.native(function(err, user) {          if(err) {                                 console.log("there no exist user _id");         }          user.findone({'_id' : req.param('id')},                  function(err, user) {                   results = user;                   console.log(results);                  // send response or make callback here         });      }); } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -