node如何得到正确的返回值
下面的代码,返回到客户端的json数组总是空,执行后也是在控制台先输出2,后输出1,明明findData(id)在前,为什么先输出了2,而不是先输出findData里的1
var findTalk = function(id){
connection.query('SELECT id,content,date,userimg,username FROM talk'+id, function(err, rows, fields) {
if (err)
throw err;
if (rows) {
for (var i = 0; i < rows.length; i++) {
var talk = {
id: 0,
content: "",
date: '',
userimg: '',
username: ''
};
talk.id = rows[i].id;
talk.content = rows[i].content;
talk.date = rows[i].date.toString().substr(0, 21);
talk.userimg = rows[i].userimg;
talk.username = rows[i].username;
talks.push(talk);
}
console.log('1');
}
});
};
app.get('/talk/', function(req, res) {
var id = req.query.id;
findTalk(id);
for (var i = 0; i < talks.length; i++) {
json[i] = talks[i];
}
console.log('2');
res.send(json);
});