2014-02-16 43 views
0

SELECT总是显示我undefined在输入文本未定义SELECT查询+的NodeJS玉

Demo

路线:

exports.edit = function(req, res){ 
    var id =(req.params.id); 
    customer = db.getCustomerById(id,function(results){ 
     res.render('customer/edit', {customer: results }); 
    }); 
}; 

DB函数:

exports.getCustomerById = function(id,callback){ 
    var objBD = BD(); 
    objBD.query('SELECT * FROM user WHERE id=? ', id, callback); 
}; 

Edit.jade:

form(id='form', method='POST', action='/customer/edit/#{customer.id}') 
    input(type='text', id='name', name='name' value='#{customer.name}') 
    input(type='email', id='email', name='email' value='#{customer.email}') 
    input(type='tel', id='phone', name='telephone' value='#{customer.phone}') 
+0

哪些是您使用的是使数据库查询模块? –

+0

@LeonelMachava mysql – rokirakan78

+0

好的。请看我的答案。 –

回答

1

db.getCustomerById传递回调的格式应为callback(err, results)。我猜results的参数将是一个数组,因此挑选客户需要做customer = results[0]

试试下面的代码:

exports.edit = function(req, res){ 
    var id =(req.params.id); 
    customer = db.getCustomerById(id,function(err, results){ 
     if (err) { 
     console.log("Ops! Error trying to get customer ...."); 
     throw err; 
     } 
     res.render('customer/edit', {customer: results[0] }); 
    }); 
}; 
+0

谢谢你完美的作品,缺少结果[0] – rokirakan78