我不知道发生了什么。当我尝试向后端发送请求以添加关注者(我的路由如下)时,我收到服务器tiemout错误,而不是成功,但是在我的数据库中,关注者被正确添加(并删除),但并非总是如此。有时它保存相同结果的3倍(追随者到db),或者有时不删除关注者。 问题是我不知道发生了什么。
在我的控制台中,我有时会看到此错误: [HPM]尝试将请求/ api / users / user / follow从127.0.0.1:8080代理到http://[::1]:1648(ECONNRESET)(https://nodejs.org/api/errors.html#errors_common_system_errors)
时发生错误setfollower路线:
const setfollowing = async (req,res,next) => {
try {
const userId = req.body.userId;
const followId = req.body.followId;
await User.findByIdAndUpdate(
userId,{
$push: {
following: followId,},);
next();
} catch (err) {
res.status(400).json({
error: err,});
}
};
const setfollower = async (req: Request,res: Response) => {
try {
const followId = req.body.followId;
const userId = req.body.userId;
const result = await User.findByIdAndUpdate(
followId,{
$push: {
followers: userId,{ new: true },)
.populate('following','_id name')
.populate('followers','_id name')
const followerResult = { ...result._doc };
const { photo,salt,passwordHash,...rest } = followerResult;
return res.status({ ...rest });
} catch (err) {
res.status(400).json({
error: err,});
}
};
router.put(
'/user/follow',isUserSignIn,setfollowing,setfollower,);
点击按钮发送请求
try {
setLoading(true);
const response = await fetch('/api/users/user/follow',{
body: JSON.stringify({
followId: params.userId,userId: loggedInUser._id,}),headers: {
'accept': 'application/json','Authorization': `Bearer ${token}`,'Content-Type': 'application/json',method: 'PUT',});
const data = await response.json();
setLoading(false);
setfollowing(true);
} catch (err) {
if (err.message) {
setServerError(err.message);
} else {
setServerError(JSON.stringify(err));
}
}