【发布时间】:2015-11-18 16:10:11
【问题描述】:
大家好,我有一个简单的处理程序,它可以读取 mysql 表并将 json obj 返回到路由,就像这样。
处理程序
var PostStore = {};
PostStore.getAllPosts = function(){
conn.query('SELECT * FROM posts',function(err, result){
if(err){
console.log(err);
}
console.log(JSON.parse(JSON.stringify(result)));
return JSON.parse(JSON.stringify(result));
});
}
module.exports = PostStore;
路由器
{
path: '/',
method: 'GET',
handler: function (request, reply) {
console.log(PostStore.getAllPosts);
reply.view('index', {
title: 'My home page',
posts: PostStore.getAllPosts
});
}
}
index.html
<h1>{{title}}</h1>
{{#each posts}}
<h1>{{this.title}}</h1>
{{/each}}
这是控制台输出的样子
[Function]
[ { id: 1,
title: 'Hello World',
body: 'My First Post on this cool Hapi Blog!',
date: null } ]
如您所见,sql 结果被解析为 JSON obj,但未从把手读取。另请注意,{{title}} 按预期显示“我的主页”。
任何帮助将不胜感激!谢谢。
【问题讨论】:
标签: node.js express handlebars.js hapijs