未定义的SELECT查询nodejs + jade

时间:2014-02-16 12:55:09

标签: node.js express pug

SELECT始终在输入文本中显示undefined

Demo

路线:

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

数据库功能:

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

1 个答案:

答案 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] });
   });
};