i sent a request to the mongodb to get some data according to email.my client side code is working properly . code is here:
useEffect(()=>{
const email = user?.email;
// console.log(email)
const url = `http://localhost:5000/services?email=${email}`;
console.log(url)
fetch(url)
.then(res => res.json())
.then(data => setItems(data))
},[user])
console.log showing the data as i expected
but the problem start with the backend. i tried console.log to get the email and other thing but the code is not showing nothing even not a single error. here is the code i wrote for api with express:
app.get('/services',async(req,res)=>{
const email = req.query.email;
console.log(email)
const query = {email:email};
const cursor = serviceCollection.find(query);
const item = await cursor.toArray();
res.send(item);
});
So far I didn't see any issue with your code.
Have you tried to call your GET request from the browser directly?
You might check on req
and res
to see the incoming data to that endpoint (/services
)
You also can try to add the request options in your fetch
method
var requestOptions = {
method: 'GET',
redirect: 'follow'
};
const email = user?.email;
const url = `http://localhost:5000/services?email=${email}`;
fetch(url , requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
And I suggest you to using Postman, to call that endpoint. It will easier to debug the API using 3rd party app like Postman.