I haven't done much work with mongo so coming from mysql it is a bit of a challenge. What I want to do is get the data for the users in my friends array ie: their name, email etc. This is what I have below. When I hit a certain route I send my ID to the api endpoint. I then need to get the friend ID's from the array and get their relevant information.
Record in the database
{
"_id": {
"$oid": "5f1b22a427e4624711bdef0e"
},
"friends": [{
"$oid": "5f206e15e9b66391246788d6"
}, {
"$oid": "5f2456777656525890dd9c21"
}],
"name": "Joe Smith",
"email": "[email protected]",
"mobile": "1233455467889",
}
Controller
exports.getAddedContacts = async (req, res) => {
const userId = req.params.uid;
let friends;
try {
friends = await User.find({ _id: userId });
} catch (err) {
res
.status(500)
.json({ error: "Something went wrong, could not get contacts" });
}
if (!friends || friends.length === 0) {
res.status(404).json({ error: "No contacts found..." });
}
res.status(200).json({ friends: friends });
};