将.exec(err,rows)中的MySQL行返回给Sails JS中的一个函数(我不想在响应中发送行)
如果我不想通过response.json()
的response.ok()
将它发送给响应,我如何返回表(模型)的行。
我有user
模型的API /模型/ User.js
module.exports = { attributes: { name:{ type:'string' }, age:{ type:'text' } } };
我正在写一个函数在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.) */ }
使用callback,或承诺。 这里是一个callback的例子:
module.exports=function sendList(model,callback){ model.find({}).exec(function(err,rows){ callback(rows); }); }
要使用它:
sendlList(model, function(rows) { console.log(rows); // Go ahead and use them now. });
加里的回应是有效的。
或者只是return
:
module.exports.sendList = function(model){ model.find().exec(function(err,rows){ if(!err) return rows; }); }