I am working on a side project, trying to up my knowledge of React.js and Node.js and I have the following code in my Node server:
export const deleteConnection = async (req, res) => {
try {
let deletedConnection = await Connection.findByIdAndDelete(
req.params.connectionId
).exec();
let user = await User.findById(req.params.userId).exec();
console.log(user);
const remainingConnections = user.connections.filter(
(c) => c.connection !== req.params.connectionId
);
console.log(remainingConnections);
res.status(200).json(deletedConnection);
} catch (err) {
console.log(err);
res.status(400).send("Unable to delete connection", err);
}
};
In the code, I have a collection called Connection and another one called User. I delete the Connection collection, and then I'm trying to filter out the Connection that matches the connectionId. However, it isn't working, and I don't know why. There are no errors in the code, and I leave screenshots below of what the server prints out. The Connection collection is deleted, but the filter is not filtering out the Connection that matches the connectionId, so I have no way of updating the User collection. I don't know if I'm missing something about Node.js that prevents it from working, but in React.js something like this would definitely work.
For reference, the userId is the string ending with bb8bd83 and the connectionId is ending with bb8bd8f. The top object in the second screenshot is the Connection that matches the connectionId (which shouldn't appear), and everything is accessed correctly (e.g. nothing prints out undefined or anything like that). Below is what the user console.log prints out:
And here is what the remainingConnections console.log prints out: