简体   繁体   中英

Return MySQL rows from .exec(err,rows) to a function in Sails JS(i dont want to send rows in response)

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.)
    */
}

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's response works.

Or just return :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM