'How to get data from server to client side based on same kind of value in react?
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?
Solution 1:[1]
According to this answer https://stackoverflow.com/a/6912872/15892381 you can access the req.query in express base apps in Node.Js you can just use url parser from url module and read the query from that:
const url = require('url');
const url_parts = url.parse(request.url, true);
const query = url_parts.query;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mohammad Hakimi |
