정성진
정성진

Reputation: 79

nodejs cannot set property '' of undefined

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

well.... this is my code. And I can't find what's wrong.. It suddenly don't work... Please find any problem for me

Upvotes: 0

Views: 1101

Answers (1)

Ankit
Ankit

Reputation: 990

Please don't overlook the asynchronous behaviour here. Send your response inside the callback function after the loop.

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

Upvotes: 3

Related Questions