2013-02-14 27 views
4

我開始使用Node.js,我完全困惑於爲什麼我無法獲得我的SQL查詢的結果來呈現頁面。當我嘗試在Jade中顯示SQL查詢結果時獲取[object Object]

connection.query('SELECT * FROM testTable', function selectCb(err, rows, fields) { 
    if (err) { 
    throw err; 
    } 
    for (var i in rows) { 
     console.log(rows[i]); 
    } 
    res.render('showResults.jade', { 
    results: rows 
    }); 
}); 

在控制檯的結果顯示完美,但是當我試圖使它們與翡翠,我得到了一些要點(等於表條目的數量),但每一個,然後按[對象,對象]。這是我的Jade文件:

h1 SQL Results are as follows: 
ul 
    each item, i in results 
    li= item 

是否有額外的步驟或我需要的結果才能正確顯示?

回答

8

你看到的輸出是因爲Jade簡單地調用.toString()表達式的結果。而且,對於Object就像itemthat's the default result

> {}.toString() 
'[object Object]' 

爲了獲得更有用的東西,你必須指定格式:

li #{item.prop1} (#{item.prop2}) 

對於JSON:

li= JSON.stringify(item) 

如果您想要使用console.log()看到的格式,您可以將f結it usesutil.inspect(),作爲視圖助手:

// Express 3 
app.locals.inspect = require('util').inspect; 
li= inspect(item) 
相關問題