Reputation: 171
So I'm trying to fetch data from another service and convert it as object, and get the specific property by ID for example, but the problem is that my data seems like it's undefined, maybe the syntax is wrong? But I wouldn't say that...
app.get ('/products&comments/:title', async (req , res) => {
const title = req.params.title;
const db = await connect();
const doc = await db.collection("posts").findOne({title: title});
const { comments } = await axios.get("http://localhost:4201/comments");
const singleProductComment = await comments.find((comments) => comments.postID === doc._id);
console.log(singleProductComment)
res.json(singleProductComment)
});
What could be wrong, isn't this syntax const singleProductComment = await comments.find((comments) => comments.postID === doc._id);
good?
The full error message:
Console.log(comments) output:
Upvotes: 0
Views: 242
Reputation: 2270
Your loop variable name 'comments' is the same as the array variable name 'comments', also you don't need to await before find.
**comments**.find((**comments**) => **comments**.postID === doc._id);
should be:
comments.find((comment) => comment.postID === doc._id);
here: comments = [comment, comment, comment, comment];
Upvotes: 3