Why my code is not saving the user but giving me everything ok in response? it’s giving me a Message marked as seen. in response when I hit the API but when I check my database I can’t see any changes the status is still sent but not seen but when I hit it give me a response ok how to fix this problem? I checked many times it does not update
router.post('/markasread', async (req, res) => {
const { RoomId, RecieverId } = req.body;
try {
// Find the recipient's document using their ID
const recipient = await User.findOne({ _id: RecieverId });
// Find the message in the recipient's "AllMessages" array using the room ID
const messageIndex = recipient.AllMessages.findIndex(
message => message.RoomId === RoomId
);
if (messageIndex !== -1) {
// Update the status of the message to "seen"
recipient.AllMessages[messageIndex].status = "seen";
await recipient.save();
}
res.status(200).send("Message marked as seen.");
} catch (err) {
console.log(err);
res.status(500).send("Error marking message as seen.");
}
});
This is my all messages array:
AllMessages: {
type: Array,
default: [],
},
and this is how i am creating objects inside it:
router.post('/message', async (req, res) => {
const { SenderId, message, RoomId, RecieverId, LastMessage, status } = req.body;
console.log(status)
try {
const newMessage = new Message({
SenderId,
message,
RoomId,
RecieverId,
timestamp: Date.now(),
});
await newMessage.save();
const date = Date.now();
const sender = await User.findOne({ _id: SenderId });
const receiver = await User.findOne({ _id: RecieverId });
const roomExistsInSender = sender.AllMessages.some(
(msg) => msg.RoomId === RoomId
);
if (!roomExistsInSender) {
sender.AllMessages.push({
SenderId,
RecieverId,
LastMessage,
RoomId,
status,
date,
});
await sender.save();
}
const roomExistsInReceiver = receiver.AllMessages.some(
(msg) => msg.RoomId === RoomId
);
if (!roomExistsInReceiver) {
receiver.AllMessages.push({
SenderId,
RecieverId,
LastMessage,
RoomId,
status,
date,
});
await receiver.save();
}
await User.updateOne(
{ _id: SenderId, 'AllMessages.RoomId': RoomId },
{
$set: {
'AllMessages.$.SenderId': SenderId,
'AllMessages.$.RecieverId': RecieverId,
'AllMessages.$.LastMessage': LastMessage,
'AllMessages.$.status': status,
'AllMessages.$.date': date,
},
},
);
await User.updateOne(
{ _id: RecieverId, 'AllMessages.RoomId': RoomId },
{
$set: {
'AllMessages.$.SenderId': SenderId,
'AllMessages.$.RecieverId': RecieverId,
'AllMessages.$.LastMessage': LastMessage,
'AllMessages.$.status': status,
'AllMessages.$.date': date,
},
},
);
res.status(200).send({ message: "Message saved" });
} catch (err) {
console.log(err);
res.status(422).send(err.message);
}
});
>Solution :
Try with findOneAndUpdate
:
router.post('/markasread', async (req, res) => {
const { RoomId, RecieverId } = req.body;
try {
const updatedUser = await User.findOneAndUpdate(
{ _id: RecieverId, 'AllMessages.RoomId': RoomId },
{
'AllMessages.$.status': 'seen',
},
{ new: true }
);
if (!updatedUser) return res.status(400).send('User not found.');
res.status(200).send('Message marked as seen.');
} catch (err) {
console.log(err);
res.status(500).send('Error marking message as seen.');
}
});