0

I am not sure how to pass in an array of IDs into Product.find(). This is basically what I have done in the NodeJS. I am able to get the data from the console log(category) and returned the _id and title. But when I tried console.log(category._id), it returned undefined. What I want to achieve is to pass the _id into the product.find with an $in operator: Product.find({catid:{$in:category.id}}) but is not working, and I am not sure how to? Greatly appreciate for any helps and many thanks in advance. Thanks again

let category  = [] 
const category  = users.category
    
console.log(category) // [{"_id":"5feb2122a8632e0550deb43b","title":"A"},{"_id":"5fedef35259fd41fec38bc70","title":"B"},{"_id":"5ff14b9ba012a42238acdcd3","title":"C"}]  
console.log(category._id) //undefined  

Product.find({catid:{$in:category.id}})

1 Answer 1

1

The category is an array, try map() to extract array of _id

const category = users.category.map(({ _id }) => _id);
Product.find({ catid: { $in: category } });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.