mysql - 在服务器端使用 express-handlebars 在 node.js 上显示字符串集合
问题描述
我有要使用 express-handlebars 显示的 sql 查询结果
var x={} ;
//friends表得到了name|profileName|status属性
dao.findFriends(req.session.UserName,function(err,rows,fields){
x=rows;
for(var i=0;i<rows.length;i++){
console.log(rows[i].profileName);
}
});
res.render('home',{body:'hello ...'+req.session.UserName,friends:x});
简单的字符串显示正常,但不是 sql 结果
<table>
<tr>
<th>Name</th>
</tr>
{{#each friends}}
<tr>
<td>{{profileName}}</td>
</tr>
{{/each}}
</table>
解决方案
找到了!必须使变量x接受 json 数组和 .. 并将所有结果推入x内 for 循环
var x =[];
dao.findFriends(req.session.UserName,function(err,rows,fields){
if(!err){
console.log('listing friends');
//list in bunch of <li> <ul> </ul>
for(var i=0;i<rows.length;i++){
console.log(rows[i].profileName);
//x = rows;
x.push({
profileName: rows[i].profileName
});
}
}
else
console.log('err finding people...');
res.render('home',{body:'hello ...'+req.session.UserName,friends:x});
});
并在 html 端引用具有此前缀的字段
<table>
<tr>
<th>Name</th>
</tr>
{{#each friends}}
<tr>
<td>{{this.profileName}}</td>
</tr>
{{/each}}
推荐阅读
- java - 在 ArrayList 中索引原始数组 - Java
- r - 合并列值相似但列值不同的行
- assembly - 将 argv 中的第一个参数与 char 进行比较
- python - 指数曲线拟合自定义数据集 - 输出是平线
- php - 我如何在php的邮件功能中发送HTML
- minizinc - 从 cpp 程序调用 minizinc
- node.js - NodeJS google-translate-api BAD_REQUEST
- c++ - 我可以将迭代器增加一个整数吗?
- javascript - Javascript - 试图在画布上的其他地方擦除和重绘线条不起作用
- mysql - 错误 1054 (42S22):“where 子句”中的未知列“年份”