Nathan Nathan - 3 months ago 17
MySQL Question

Node.js/jade -- How can I pass mysql data as a local variable into inline javascript?

My Node script has this in it:

var connection = mysql.createConnection(...);

connection.connect();

connection.query(/*sql query*/, function(err, rows, fields){
app.get('/', function(req, res){
res.render('index', { data: JSON.stringify(rows) });
});
});


Then if I do this in my Jade template:

body
p !{data}


It displays the data from the MySql query exactly as you'd expect. But if instead I do:

body
script(type='text/javascript').
console.log(!{data});


It gives me
[Object, Object, Object, Object....


Why is it interpreted differently if it's part of the client Javascript? And how can I fix this?

I put JSON.stringify in the local variable assignment because if I didn't, nothing would get passed through no matter where in the template I tried to put it. Is there another way I'm supposed to be transforming the data maybe?

Answer

You should have to give an index like console.log(!{data[1]}); to view the objects in console..

Comments