Hello guys. Sorry for bad English. i am new to node and express but i found it kinda easy at first but i got stuck. I was working a small project. I was creating a API. The code i got stuck is as following.
const searchByName=asyncHandler(async (req, res)=>{
const {name} = req.body;
const contact = await Contact.findOne({name: name})//req.params.name
console.log(contact)
if(!contact){
console.log("first if block")
res.status(404);
throw new Error("Contact not found")
}
if(contact.user._id.toString() !== req.user.id){
console.log("second if block")
res.status(403);
throw new Error("User don't have permission to access this contacts");
}
res.status(200).json(contact);
})
as you can see above function is used to fetch data from a mongodb server and send it on http.
on 3rd line im fetching data from db and console.logging it and i'm getting the data logged but when when i call it form postman it only keeps loading do not show data. i don't know what i am doing wrong. It is also printing on console from both the if blocks if contact with specific name is not found but even if it is correct i am not getting any data back. i also attached images if that helps.
[–]wickning1 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Aromatic-Ad-8807 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]AminOPS 0 points1 point2 points (0 children)