1

How can i return the rows of a table (model) if i don't want to send it to response via response.json() of response.ok().

i have user model api/model/User.js

module.exports = {
  attributes: {
    name:{
      type:'string'
    },
    age:{
      type:'text'
    }
  }
};

I am writing a function in api/services/sendList.js

module.exports=function sendList(model){
  model.find({}).exec(function(err,rows){
   //here i dont want to send it as res.json(rows)
  });
   /***i want to return the rows obtained so that it can be used
    *somewhere else(wherever the function **sendList** is being
    *called.)
    */
}
vkstack
  • 1,582
  • 11
  • 24

2 Answers2

1

Use a callback, or promises. Here is a callback example :

module.exports=function sendList(model,callback){
  model.find({}).exec(function(err,rows){
    callback(rows);
  });
}

To use it :

sendlList(model, function(rows) {
    console.log(rows);

    // Go ahead and use them now.
});
Gary
  • 2,866
  • 1
  • 17
  • 20
0

Gary's response works.

Or just return:

module.exports.sendList = function(model){
  model.find().exec(function(err,rows){
    if(!err) return rows;
  });
}
  • 1
    That will not work... sendList will return before results are available. – Gary Dec 18 '15 at 13:52
  • console.log(getList(User)); and this was printing undefined – vkstack Dec 18 '15 at 13:55
  • If you don't know why this does not work, read http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 – Gary Dec 18 '15 at 13:56