下面的路由正在生成一个弃用消息和一个范围错误。我一直在看这两本书,但还是不懂。
// Update
app.put('/blogs/:id', (req, res) => {
Blog.findByIdAndUpdate(
req.params.id,
req.body.blog,
function (err, updatedBlog) {
if (err) {
console.log(err);
// res.redirect('/blogs');
} else {
res.redirect('/blogs/' + req.params.id, { blog: updatedBlog });
}
}
);
});
下面是错误信息的完整版本
express deprecated res.redirect(url, status): Use res.redirect(status, url) instead app.js:89:13
events.js:292
throw er; // Unhandled 'error' event
^
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: { blog: [Object] }
at ServerResponse.writeHead (_http_server.js:248:11)
at ServerResponse._implicitHeader (_http_server.js:239:8)
at write_ (_http_outgoing.js:642:9)
at ServerResponse.end (_http_outgoing.js:753:5)
at ServerResponse.redirect (/home/jimmy/code/courses_tutorials/js_scratchpad/node_demos/blog_REST_demo/node_modules/express/lib/response.js:951:10)
at /home/jimmy/code/courses_tutorials/js_scratchpad/node_demos/blog_REST_demo/app.js:89:13
at /home/jimmy/code/courses_tutorials/js_scratchpad/node_demos/blog_REST_demo/node_modules/mongoose/lib/model.js:4837:16
at /home/jimmy/code/courses_tutorials/js_scratchpad/node_demos/blog_REST_demo/node_modules/mongoose/lib/query.js:2869:28
at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on Function instance at:
at /home/jimmy/code/courses_tutorials/js_scratchpad/node_demos/blog_REST_demo/node_modules/mongoose/lib/model.js:4839:13
at /home/jimmy/code/courses_tutorials/js_scratchpad/node_demos/blog_REST_demo/node_modules/mongoose/lib/query.js:2869:28
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
code: 'ERR_HTTP_INVALID_STATUS_CODE'
}
[nodemon] app crashed - waiting for file changes before starting...
谁能解释一下这个错误并给我指出正确的方向?感谢所有!res.redirect
不接受对象作为参数。它可以选择一个状态(例如:302
),这可能是函数试图解析您的对象为。
相反,您可以将用户重定向到查看已编辑的博客:
res.redirect('/blogs/' + req.params.id);
错误来自于传递的不正确参数。首先应该是状态,其次是url。当前,您正在传递url,状态应该是url和某种对象到url参数