简体   繁体   English

res.send(status,body):改用res.status(status).send(body)

[英]res.send(status, body): Use res.status(status).send(body) instead

I'm getting this error on validating a form how can I resolve it. 我在验证表单时遇到此错误,该如何解决。

code: 码:

  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})
})

error: 错误:

  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)

I believe you have a res.send() where you intended a res.render() 我相信您在打算使用res.render()的地方有一个res.send()

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})
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 表示不推荐使用的 res.send(status, body):即使表达式更改,使用 res.status(status).send(body) 也不起作用 - express deprecated res.send(status, body): Use res.status(status).send(body) does not work even when expresion change node-express 错误:表示已弃用 res.send(status):请改用 res.sendStatus(status) - node-express error : express deprecated res.send(status): Use res.sendStatus(status) instead res.status(400) 使我的链接 res.send(“this message”) 不会出现在客户端 - res.status(400) makes my chained res.send(“this message”) not show up on client side NodeJS Express Api——在路由外部调用 res.send 有效但 res.status 无论如何都不起作用 - NodeJS Express Api -- calling res.send outside route works but res.status does not work no matter what 发送res.status(409).send('error')不起作用 - doesn't work send in res.status(409).send('error') res.status().send() 在 Promise.all 中无法正常工作 - res.status().send() not working correctly in Promise.all 节点/ Express-res.status()。send()仅发送状态,不发送对象 - Node/Express - res.status().send() only sends the status but does not send an object 类型错误:res.status 不是 function - TypeError: res.status is not a function NodeJS res.status 不是一个函数 - NodeJS res.status is not a function res.status 不是 function - express - res.status is not a function - express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM