我想從函數selectpro獲取返回值,但該值始終爲空。如何使用yield來返回函數中的值?
這是函數selectpro:
exports.selectpro = function(){
var sql = "SELECT DISTINCT projectname FROM invoketable";
var projectname;
client.query(sql,function(err,result){
if(err) throw err;
projectname = result;
console.log('select projectname from db');
//console.log(projectname); //undefined
//return projectname;
});
console.log(projectname); //undefined
return projectname;
}
時調用的函數selectpro:
var mysqlinsertp = require('./public/mysqloperate.js');
route.get('/readproject',function *(next){
var dataprodect = mysqlinsertp.selectpro();
this.body={status:200,ok:true,data:dataprodect};
console.log(this.body);
});
我得到的回報是不確定的。我嘗試使用'yield',但是當我使用mysqlinsertp.selectpro()。next()來調用函數時,它返回'next()未定義'。
其中你調用var result = xx.selectpro()?? – Subburaj
@Subburaj我調用名爲routes.js的文件中的函數和名爲readdb.js的文件中定義的'exports.selectpro = function(){}' – gdreamlend
這行代碼是什麼:'console.log(projectname);'show您? – AlexD