2014-02-16 72 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