Untitled
unknown
javascript
3 years ago
1.6 kB
9
Indexable
/*
* URL /photosOfUser/:id - Return the Photos for User (id)
*/
app.get("/photosOfUser/:id", async function (request, response) {
try {
const id = request.params.id;
// Find all photos that created by this user
const photos = await Photo.find({
user_id: mongoose.Types.ObjectId(id),
});
if (!photos) {
return response.status(404).send("Missing user");
}
const listComments = [];
for (photo of photos) {
// If this photo has no comments, then ignore it
const isOk =
Array.isArray(photo.comments) && photo.comments.length > 0;
if (!isOk) {
continue;
}
// Loop through comments and find the user information
for (const comment of photo.comments) {
const user_id = comment.user_id;
const user = await User.findById(user_id);
listComments.push({
id: comment._id,
user_id: user._id,
comment: comment.comment,
date: comment.date_time,
user: {
first_name: user.first_name,
last_name: user.last_name,
},
});
}
}
response.json({ user_id: id, comments: listComments });
} catch (error) {
console.log(error);
return response.status(500).send("Error");
}
});
Editor is loading...