javascript - How to iterate and parse JSON from query in Express/Jade? -
i new nodejs i'm thinking simple cannot find way pass json data select query jade view. using node.js tools visual studio create project uses express + jade. here index.js
file:
exports.products = function (req, res) { var request = new sql.request(connection); console.log("connection successful."); request.query('select * product', function (err, data) { console.log(data[i]); res.render('products', { title: 'products', year: new date().getfullyear(), message: 'products page devices', name: 'rodney', result: data[i] }); });
this view i'm using in jade render results:
- product = typeof(result) != 'undefined' ? result : { } p in product h3 #{product.name}
i getting error i not defined
makes sense, can show 1 record if change result: data[0]
. here sample of how json structured: { id: 56, name: 'motion', modelstring: '1234-g', description: 'motion front door', released: true, boardid: 29, createdate: wed aug 05 2015 06:29:31 gmt-0500 (central daylight time), serialnumbercode: 'aaa', extensionid: 9, sku: '1234-g', currenttest: false, modelencodingnumber: -1 }
how can show records column name
?
you can directly forward data res.render
method this:
res.render('products', { ... products: data }); // in view.jade p in product h3 #{product.name}
if need names of product, can loop on data first, collect names , store them in list:
var allnames = data.map(function(element) { return element.name; }) res.render('products', { ... names: allnames }); // view.jade name in names h3 #{name}
Comments
Post a Comment