Getting [object Object] when I try to display SQL query results in Jade

I am starting to work in Node.js, and I am completely confused about why I cannot get the results of my SQL query for rendering on the page.

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

The results are displayed perfectly in the console, but when I try to display them using Jade, I get a few bullet points (equal to the number of entries in the table), but each of them follows the object [object, Object], This is my Jade file:

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

Is there an extra step or something I need to display the results correctly?

+4
source share
1 answer

You see that Jade just calls .toString() as a result of the expression. And for Object as item , that is the default result :

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

To get something more useful, you need to specify the format:

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

For JSON:

 li= JSON.stringify(item) 

If you need the format that you see using console.log() , you can open the function it uses , util.inspect() , as an assistant of the form:

 // Express 3 app.locals.inspect = require('util').inspect; 
 li= inspect(item) 
+8
source

All Articles