0

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?

MORÈ
  • 2,480
  • 3
  • 16
  • 23
  • I don't see how that could be `undefined`. It might be `"undefined"` because the value returned by `useAuthState` is `undefined` and that gets stringified by `http://localhost:5000/useritems?email=${email}`. I don't know what `useAuthState` is though or why you expect it to return something other than `undefined`. – Quentin May 08 '22 at 07:55

1 Answers1

-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;