【发布时间】:2015-06-08 02:53:29
【问题描述】:
刚开始使用 Node.js,现在我成功配置了 Postgres 数据库。我可以从 db 中获取记录,也可以在 console.log 中查看记录。 但是现在我有点卡住了如何将数据发布回同一页面。
我有一个名为 index 的页面,其中包含两个文本字段和一个提交按钮。当我使用用户名和 ID 提交数据时,我会在 request.post 方法中收到此请求。
router.post('/', function (req, res) {
var username = req.body.username;
var userid = req.body.userid;
// method which take parameters and return records from db
getRecords(userid, username, function (result, found) {
if (found) {
console.log(result.rows.length);
}
});
});
从数据库成功传输后,我可以在控制台中看到记录数。但是现在我如何将这些数据发布到我收到此请求的同一页面(index.ejs)中。
我试过了
res.send('/', { title: result.rows.length });
在 ejs 文件中
<p>Count is <%= title %></p>
但我收到“标题”未定义的错误。 那么如何显示计数以及如何显示从数据库中获取的记录列表。
【问题讨论】:
标签: javascript node.js postgresql postback