[英]Return MySQL rows from .exec(err,rows) to a function in Sails JS(i dont want to send rows in response)
如果我不想通過response.json()
的response.ok()
將其發送到響應表,該如何返回表(模型)的行。
我有user
模型api / model / 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.)
*/
}
使用回調或Promise。 這是一個回調示例:
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;
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.