I’m using mongo db and angular for an eCommerce application.
Requirement is to get items under each user in cart.
Here while fetching I’m using the objectId as the reference to find the data, Its not finding out any data from the DB.
exports.findAllCartItems = (req, res) => {
const id = req.params.id;
insertTocartItem.find(id)
.then(data => {
if (!data)
res.status(404).send({ message: "Cart Is empty" });
else res.send([data]);
})
.catch(err => {
res
.status(500)
.send({ message: "Error while showing Cart" });
});
};
Data In collection:
"_id": "$oid": "6347caf515a675847127b020"
"parentcategoryId": 1000,
"brandName": "Nothing",
"modelName": "1",
"Price": "30000",
"itemsImage": "http://localhost:8080/public/1664539223328--Nothing_phon.jpg",
"itemsubCatId": "7000",
"itemId": "7000MB1",
***"userInfoId": "634677b7ce60d9d8cd591940",***
"__v": 0
I’m passing userInfoId": "634677b7ce60d9d8cd591940 from frontend, I’m able to see the ID in req.params.id but unable to find the matching data.
If changed insertTocartItem.find(id) to insertTocartItem.find({id}) it is fetching entire data in the cart irrespective of userInfoId.
Kindly help with any method to find the data based on userInfoId.
4
Answers
After using async/await it's working fine, Thanks for all the suggestions.
You need to instruct in the query which field you want to search.
See you have to pass
id
in_id
property to find the data like this,The thing is mongoose schema is an object based right so in
find()
method you have to pass schema property key that is_id
and value that isreq.params.id
.Or If you dont want to pass an object then you can use
findById()
method like this,You should indicate which field you wanna apply the request to…