我刚从node.js开始,在CRUD方面遇到了一些问题,尤其是在更新MongoDb中数据的PUT方法方面。
在我的终端中,错误显示为:
-
在更新功能中,
-
在以下功能中
节点:事件:368投掷者;//未处理的"错误"事件^
错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头在新的NodeError(节点:内部/错误:371:5(在ServerResponse.setHeader(节点:_http_outgoing:576:11(位于ServerResponse.header(/Documents/BookFace/server/node_modules/express/lib/response.js:776:10(位于ServerResponse.send(/Documents/BookFace/server/node_modules/express/lib/response.js:170:12(位于ServerResponse.json(/Documents/BookFace/server/node_modules/express/lib/response.js:267:15(在/Documents/BookFace/server/controllers/user.controller.js:75:37位于/Documents/BookFace/server/node_modules/mongoose/lib/model.js:4961:18在processTicksAndRejections(节点:internal/process/task_queues:78:11(在以下位置对函数实例发出"error"事件:位于/Documents/BookFace/server/node_modules/mongoose/lib/model.js:4963:15在processTicksAndRejections(节点:internal/process/task_queues:78:11({代码:'ERR_HTTP_HEADERS_SENT'}
这是代码:
const UserModel = require('../models/user.model')
const ObjectID = require('mongoose').Types.ObjectId
module.exports.getAllUsers = async (req, res) => {
//send all details about all users except the password
const users = await UserModel.find().select('-password')
res.status(200).json(users)
}
module.exports.userInfo = (req, res) => {
//params = the param which is written into the url
/* console.log(req.params) */
if (!ObjectID.isValid(req.params.id))
return res.status(400).send('Id unknown : ' + req.params.id)
UserModel.findById(req.params.id, (err, docs) => {
if (!err) res.send(docs)
else console.log('ID unknown : ' + err)
}).select('-password')
}
module.exports.updateUser = async (req, res) => {
if (!ObjectID.isValid(req.params.id))
return res.status(400).send("ID unknown : " + req.params.id);
try {
await UserModel.findOneAndUpdate(
{ _id: req.params.id },
{
$set: {
bio: req.body.bio,
},
},
{ new: true, upsert: true, setDefaultsOnInsert: true },
(err, docs) => {
if (!err) return res.send(docs);
if (err) return res.status(500).send({ message: err });
}
);
} catch (err) {
return res.status(500).json({ message: err });
}
}
module.exports.deleteUser = async (req, res) => {
if (!ObjectID.isValid(req.params.id))
return res.status(400).send("ID unknown : " + req.params.id);
try {
await UserModel.deleteOne({ _id: req.params.id }).exec()
res.status(200).json( { message: 'succefully deleted.'})
}
catch(err){
return res.status(500).json({message: err})
}
}
module.exports.follow = async (req, res) => {
if (
!ObjectID.isValid(req.params.id) ||
!ObjectID.isValid(req.body.idToFollow)
)
return res.status(400).send("ID unknown : " + req.params.id);
try {
// add to the follower list
await UserModel.findByIdAndUpdate(
//id of the user
req.params.id,
//id of the user to follow
{ $addToSet: { following: req.body.idToFollow } },
{ new: true, upsert: true },
(err, docs) => {
if (!err) res.status(201).json(docs);
else return res.status(400).json(err);
}
);
//add to following list of the user followed
await UserModel.findByIdAndUpdate(
req.body.idToFollow,
{ $addToSet: { followers: req.params.id } },
{ new: true, upsert: true },
(err, docs) => {
// if (!err) res.status(201).json(docs);
if (err) return res.status(400).json(err);
}
);
} catch (err) {
return res.status(500).json({ message: err });
}
};
module.exports.follow = async (req,res) => {
if (!ObjectID.isValid(req.params.id) || !ObjectID.isValid(req.body.idToFollow))
return res.status(400).send('ID unknown : ' + req.params.id);
try {
// add to the follower list
await UserModel.findByIdAndUpdate(
req.params.id,
{$addToSet: {following: req.body.idToFollow}},
{new:true,upsert:true})
//(err,docs)=> {
// if (!err) res.status(201).json(docs);
//else return res.status(400).json(err);
//}
.then((docs) => res.send(docs))
.catch((err) => res.status(500).send({message:err}));
// add to following list
await UserModel.findByIdAndUpdate(
req.body.idToFollow,
{$addToSet: {followers: req.params.id}},
{new:true,upsert:true})
.catch((err) => res.status(500).send({message:err}));
} catch(err) {
return res.status(500).json({message:err});
}
};