我有想要使用express-handlebar显示的sql查询结果
var x={} ;//friends表获取名称|profileName|状态属性
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>发布于 2018-07-16 19:51:34
找到了!必须使变量x接受json数组和..并将所有结果推送到循环中的x中。
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}}
https://stackoverflow.com/questions/51360461
复制相似问题