gpt4 book ai didi

node.js - res.send(status, body) : Use res. status(status).send(body) 代替

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:25 28 4
gpt4 key购买 nike

我在验证表单时遇到此错误,如何解决它。

代码:

  app.post('/',[
check('username','Error occured in Username').trim().isEmail(),
check('password','Error occured in Password').trim().isLength({min:5})
], (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors.mapped())
res.send('index',{title: "Create New User", errors: errors.mapped()})
}
// console.log(req.body.username);
res.render('about',{user: req.body})
})

错误:

  express deprecated res.send(status, body): 
Use res.status(status).send(body) instead main.js:78:8
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: index
at ServerResponse.writeHead (_http_server.js:209:11)
at ServerResponse._implicitHeader (_http_server.js:200:8)
at write_ (_http_outgoing.js:585:9)
at ServerResponse.end (_http_outgoing.js:702:5)
at ServerResponse.send (D:\nodejs\node_modules\express\lib\response.js:221:10)
at ServerResponse.json (D:\nodejs\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (D:\nodejs\node_modules\express\lib\response.js:158:21)
at app.post (D:\nodejs\main.js:78:8)
at Layer.handle [as handle_request] (D:\nodejs\node_modules\express\lib\router\layer.js:95:5)
at next (D:\nodejs\node_modules\express\lib\router\route.js:137:13)
{ username:
{ value: '',
msg: 'Error occured in Username',
param: 'username',
location: 'body' },
password:
{ value: '',
msg: 'Error occured in Password',
param: 'password',
location: 'body' } }
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: index
at ServerResponse.writeHead (_http_server.js:209:11)
at ServerResponse._implicitHeader (_http_server.js:200:8)
at write_ (_http_outgoing.js:585:9)

最佳答案

我相信你有一个 res.send() ,而你想要的地方是 res.render()

app.post('/',[
check('username','Error occured in Username').trim().isEmail(),
check('password','Error occured in Password').trim().isLength({min:5})
], (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors.mapped())
res.render('index',{title: "Create New User", errors: errors.mapped()})
}
// console.log(req.body.username);
res.render('about',{user: req.body})
})

关于node.js - res.send(status, body) : Use res. status(status).send(body) 代替,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57405888/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com