【发布时间】:2018-08-11 14:31:53
【问题描述】:
我正在用 nodejs 构建一个 api 来与客户端(android)和管理员(web)交互。
当 api 启动时,它对管理员工作正常,并且视图可以正确呈现,但是当我将客户端连接到 api 时,我在服务器控制台中收到错误/警告,例如:
App at port 4003
db connection opened successfully
Categroies Count: 2
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:494:11)
at ServerResponse.setHeader (_http_outgoing.js:501:3)
at ServerResponse.header
(E:\nodeCMSApp\node_modules\express\lib\response.js:767:10)
at ServerResponse.json
(E:\nodeCMSApp\node_modules\express\lib\response.js:264:10)
at Categories.find.select.exec.then.data
(E:\nodeCMSApp\routes\admin_categories.js:20:22)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:13880) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): Error: Can't set headers after they are
sent.
(node:13880) [DEP0018] DeprecationWarning: Unhandled promise rejections
are deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with
a non-zero exit code.
这是我的 api 代码 sn-p:
router.get('/', (req, res) => {
Categories.find({})
.select('title slug image _id')
.exec()
.then(data => {
if (data) {
res.status(200)
.json({
success: true,
count: data.length,
categories: data
})
// I understand that the problem lies here
res.render('admin/all_categories', {
categories: data
});
} else {
res.render('all_categories', {
categories: null
});
}
})
.catch(error => {
console.error(error);
res.send('Error 404');
});
});
我知道这是因为我已经使用响应对象渲染了一个视图,并且我再次调用它来为客户端返回一些 json。
我的问题是如何渲染视图并同时为客户端返回 json 数据而不会出现任何错误? 谢谢。
【问题讨论】:
标签: json node.js promise response ejs