I'm facing some issues while updating a field in my mongoose model. My intention is to locate a specific username within a list of friends in a user model and then proceed to update a field within the same object that contains the corresponding username.
Below is the code snippet I've tried, however, the status field doesn't seem to update as expected:
router.post('/profile/:id/friendRequest/:username', authenticateUser)
router.post('/profile/:id/friendRequest/:username', async (req, res) => {
const { id, username } = req.params;
const { status } = req.query;
try {
const user = await User.findOneAndUpdate({_id: id, 'friends.username': username}, {$set:
{'friends.$.status': status}}, {new:true, upsert: true}).exec();
res.json({
friends: user.friends,
success: true,
loggedOut: false,
})
} catch (err) {
catchError(res, err, 'Invalid user id');
}
});
The section of my schema where I am attempting to update the status is outlined below:
User({
friends: {
status: Number,
username: String,
state: String
}
});
I would greatly appreciate any guidance or assistance to help me progress in the right direction.