nodejs无法设置未定义的属性“”

qzwqbdag  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(410)
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,
    });
 });

好。。。。这是我的密码。我也找不到问题所在。。突然不行了。。。请帮我找出任何问题

gstyhher

gstyhher1#

请不要忽略这里的异步行为。在循环之后在回调函数中发送响应。

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
        });
    });
});

相关问题