Node.js 节点js服务器在PUT请求后崩溃
发出put请求后,应用程序因错误而崩溃Node.js 节点js服务器在PUT请求后崩溃,node.js,mongodb,express,mongoose,put,Node.js,Mongodb,Express,Mongoose,Put,发出put请求后,应用程序因错误而崩溃 [nodemon] restarting due to changes... [nodemon] starting `node index.js` (node:21666) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useN
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
(node:21666) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use `node --trace-deprecation ...` to show where the warning was created)
Server up and running
(node:21666) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify
node:internal/process/promises:227
triggerUncaughtException(err, true /* fromPromise */);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:329:5)
at ServerResponse.setHeader (node:_http_outgoing:572:11)
at ServerResponse.header (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:771:10)
at ServerResponse.json (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:264:10)
at ServerResponse.send (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:158:21)
at /Users/kayode/Desktop/learn-mern/auth/routes/users.js:36:24
at processTicksAndRejections (node:internal/process/task_queues:94:5) {
code: 'ERR_HTTP_HEADERS_SENT'
}
[nodemon] app crashed - waiting for file changes before starting...
这是我的密码
const router = require('express').Router();
const User = require('../models/User')
router.put('/:email', async (req, res, next) => {
res.send(req.params._id)
let email = req.params.email;
let user = {
name: req.body.name,
email: req.body.email,
password: req.body.password
}
try {
await User.findOneAndUpdate(email, user);
res.status(200).send(JSON.stringify('200'));
console.log('Here first')
} catch (err) {
res.status(400).send(err) //not commented
}
})
module.exports = router;
但是,如果我尝试发出另一个请求(使用postman),而不刷新服务器,它会说发送错误无法发送请求
但是,在第36行,“at/Users/kayode/Desktop/learn mern/auth/routes/Users.js:36:24”,上面写着res.status(400).send(err)//未评论
如果我将行注释掉(即不返回错误),PUT请求将成功,之后的另一个请求也将在不刷新或重新启动服务器的情况下工作
我知道我可以评论这句话,然后继续,但我需要知道发生了什么,谢谢 您在请求开始时发送响应,并在请求结束时再次尝试发送响应(第5行)
删除此行
res.send(请求参数_id)
。仅当返回对数据库的请求时才调用res.send()。你也能分享一下你当时为什么这么做(res.send(req.params._id))吗?@joyce obi,谢谢!那是我的一个愚蠢的打字错误。我用这个id找到了一个用户,又改回使用电子邮件,但忘了编辑它。哦,好吧,很好,它已经被修复了。
const router = require('express').Router();
const User = require('../models/User')
router.put('/:email', async (req, res, next) => {
let email = req.params.email;
let user = {
name: req.body.name,
email: req.body.email,
password: req.body.password
}
try {
await User.findOneAndUpdate(email, user);
res.status(200).send(JSON.stringify('200'));
console.log('Here first')
} catch (err) {
res.status(400).send(err) //not commented
}
})
module.exports = router;