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.findOneAndUpdate(
req.params.id,
{ $addToSet: { following: req.body.idToFollow } },
{ new: true, upsert: true }
.then((data) => res.send(data))
.catch((err) => res.status(500).send({ message: err }))),
// add to following list
await UserModel.findOneAndUpdate(
req.body.idToFollow,
{ $addToSet: { followers: req.params.id } },
{ new: true, upsert: true }
.then((data) => res.send(data))
.catch((err) => res.status(500).send({ message: err })))
} catch (err) {
return res.status(500).json({ message: err });
}
};
module.exports.unfollow = async (req, res) => {
if (
!ObjectID.isValid(req.params.id) ||
!ObjectID.isValid(req.body.idToUnfollow)
)
return res.status(400).send("ID unknown : " + req.params.id);
try {
await userModel.findOneAndUpdate(
req.params.id,
{ $pull: { following: req.body.idToUnfollow } },
{ new: true, upsert: true }
.then((data) => res.send(data))
.catch((err) => res.status(500).send({ message: err }))),
// Retirer de la liste des followers
await userModel.findOneAndUpdate(
req.body.idToUnfollow,
{ $pull: { followers: req.params.id } },
{ new: true, upsert: true }
.then((data) => res.send(data))
.catch((err) => res.status(500).send({ message: err })))
} catch (err) {
return res.status(500).json({ message: err });
}
}
I code with nodejs and I use mongoDB
in order to design a social network and I am faced with a problem… Indeed at the level of my controllers I edited a controller for the follow and unfollow between users the purpose being to retrieve and verify the identifiers two users (one who wants to subscribe to a user and the other one with whom the first subscribes). who wants to subscribe sees his table "following" recover the identifier of his host and at the latter it is the table "followers" that recovers the identifier of his subscriber. To do so I did in try-catch but when I make my request on postman (request of type patch) I am always returned to catch (err){. . . }. It follows that I do not understand what is happening and I ask for your expertise thank you in advance
I tried to change FindOneAndUpdate by FindByIdAndUpdate visibly that was not the problem
3
Answers
already done except that I have a second problem with my unfollow controller. On postman when I launch the request it is mentioned to me that the identifiers I entered are not recognized in other term the condition of my if is taken into account I do not understand the reason the code is higher
};
The reason your code is being caught in the
catch(err)
block is because you have syntax issues in thetry
block.When using
async/await
syntax pattern you don’t need to use.then()
or.catch()
functions to handle the settled promises, theawait
keyword can handle that for you if you assign the returned value from the promise into a variable. To add to the confusion, your code is chaining thethen()
to one of the option objects instead of thefindOneAndUpdate
function.Change this:
to this:
I have kept the response using
res.status(201).json
until after both of your updates have settled. You were usingres.send(data))
in the first update so the second update would never have taken place as Express will have stopped execution and returned a response.You would probably want to handle each one of these updates in their own
try{}catch(err){}
blocks because if the first one failed you want to let the user know straight away and return a response. That way the second update doesn’t get executed but I’ll leave that up to you.You have a typo in your
if
statement.You need to use
ObjectId.isValid()
instead ofObjectID.isValid()
. Note you are using uppercase ‘ID’ when it should be ‘Id’.