I am trying to get data from server to my client side based on a value.Those datas email address matched I only want those to get. But I can not get the data and in the server side the email value is showing undefined. My client side code is given below where I am trying to get data and send email to the server side.
This is my client side code:
const Myproducts = () => {
const [user] = useAuthState(auth);
const [myproducts, setMyproducts] = useState([]);
useEffect(() => {
const email = user?.email;
fetch(`http://localhost:5000/useritems?email=${email}`)
.then(res => res.json())
.then(data => setMyproducts(data))
}, []);
}
And my server side code is given below where I am trying to get data from mongodb and console.log(email)
. But the email is showing undefined
.
This is my server side code:
app.get('/useritems', async (req, res) => {
const email = req.query.email;
console.log(email);
const query = { email: email };
const cursor = productCollection.find(query);
const products = await cursor.toArray();
res.send(products);
});
What's wrong with my code. How can I solve this problem? Any tips to solve this problem?