im having an error using put to update its working but throwing an error 404 user id not found
const express = require("express");
const router = express.Router();
const { client } = require("../db/db"); // Import MongoDB client
const { ObjectId } = require("mongodb");// update user by ID
router.post("/users/:id", async (req, res) => {
const id = req.params.id;
const { name, username, email, role } = req.body;
console.log(`Received request to update user with ID: ${id}`);
try {
const collection = client.db("youtubeIconiq").collection("users");
const filter = { _id: new ObjectId(id) };
const updateDoc = {
$set: { name, username, email, role },
};
const options = {
returnDocument: "after", // this is new !
};
const updatedUser = await collection.updateOne(filter, updateDoc, options);
if (!updatedUser.value) {
console.log(`User with ID ${id} not found.`);
return res.status(404).json({ error: "User not found" });
}
res.json({ message: "User updated successfully", user: updatedUser.value });
} catch (err) {
console.error(`Error updating user with ID ${id}:`, err);
res.status(500).json({ error: "Internal Server Error" });
}
});
error:
Received request to update user with ID: 668bd4be04bc387ffa7d9af2
User with ID 668bd4be04bc387ffa7d9af2 not found.
console.log(filter) //to check filter if its working
console.log(id) //to check if there is no id
console.log(updateDoc) //to check theres a problem sending the data
console.log(updatedUser) //to check the funxction is reponding
all of this give a data still doesnt know why it throwing 404 user id not found