So I have a list of data here and I’m trying to get the data who’s id is equal to 64376dc4e963ba36550b7fbb
{
"_id": "643774e0864188c2e202dddc",
"email": {
"_id": "64376dc4e963ba36550b7fb4",
"firstName": "test",
"lastName": "test",
"email": "[email protected]"
},
},
{
"_id": "643798e6864188c2e202e5c4",
"email": {
"_id": "64376dc4e963ba36550b7fbb",
"firstName": "test",
"lastName": "test",
"email": "[email protected]"
},
},
{
"_id": "64379916864188c2e202e5ff",
"email": {
"_id": "64376dc4e963ba36550b7fbb",
"firstName": "test",
"lastName": "test",
"email": "[email protected]"
},
},
{
"_id": "64379a1e864188c2e202e660",
"email": {
"_id": "64376dc4e963ba36550b7fbb",
"firstName": "test",
"lastName": "test",
"email": "[email protected]"
},
}
This is my URL
http://localhost:3000/api/completed/getSurveyByUser?userId=64376dc4e963ba36550b7fbb
I tried using this
Controllers.js
export const GetRecentSurveyByUser = async (req, res) => {
const userId = req.query.userId
try {
const completed = await Completed.find({
"email._id": userId,
}).populate({ path: "email", select: "firstName lastName email" });
if (completed.length === 0) {
return res.status(404).json({ message: "No data found" });
}
res.status(200).json(completed);
} catch (error) {
res.status(400).json(error);
}
};