var list = [
{
title : '',
author : '',
content : '',
}
]
router.get('/japan',function(req,res){
var sql = 'select * from japan';
conn.query(sql,function(err,rows,fields){
for(var i = 0 ; i<rows.length;i++){
list[i].title = rows[i].title;
list[i].author = rows[i].author;
list[i].content = rows[i].content;
}
});
res.render('menu/japan/jp',{
status : req.signedCookies.login_status,
lists : list,
});
});
好吧... 这是我的代码。 而且我找不到问题所在。 突然不起作用了... 请为我找到任何问题
答案 0 :(得分:3)
请不要在这里忽略异步行为。循环后,将您的响应发送到回调函数中。
var list = [
{
title: '',
author: '',
content: ''
}
]
router.get('/japan', function (req, res) {
var sql = 'select * from japan';
conn.query(sql, function (err, rows, fields) {
for (var i = 0; i < rows.length; i++) {
list[i] = {
title: rows[i].title,
author: rows[i].author,
Content: rows[i].content
}
}
res.render('menu/japan/jp', {
status: req.signedCookies.login_status,
lists: list
});
});
});