我是新手,正在学习Node.js,所以我认为这很简单,但我无法找到一种方法将从select查询中获取的JSON数据传递给Jade视图。我使用Node.js Tools for Visual Studio创建项目,该项目使用Express + Jade。下面是我的index.js
文件:
exports.products = function (req, res) {
var request = new sql.Request(connection);
console.log("Connection successful.");
request.query('SELECT * FROM product', function (err, data) {
console.log(data[i]);
res.render('products', {
title: 'Products',
year: new Date().getFullYear(),
message: 'Products page with all devices',
name: 'Rodney',
result: data[i]
});
});
这是我在Jade中使用的视图来呈现结果:
- product = typeof(result) != 'undefined' ? result : { }
for p in product
h3 #{product.Name}
我遇到了错误i未定义,如果我把它改成
result:data [0]
,那就只能显示一条记录。这是JSON的结构示例:
{ 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 }
如何展示所有带有列name
的记录?
i
定义在哪里? - Ruan Mendes