1

This is the front-end code which is used for sending access token to server site.

useEffect(() => {
        const getProducts = async () => {
            try {
                const url = `http://localhost:5000/product?email=${user.email}`
                const { data } = await axios.get(url, {
                    headers: {
                        authorization: localStorage.getItem('accessToken')
                    }
                });
                setProducts(data);
            } catch (err) {
                const status = err.response.status;
                if (status === 401 || status === 403) {
                    signOut(auth);
                    navigate('/login');
                    localStorage.removeItem('accessToken')
                    toast.error(err.response?.data?.message);
                }
            }
        }
        getProducts();
    }, [user.email]);

This is server site express code for response. Why every time it is receiving two request and sending two response?

app.get('/product', verifyToken, async (req, res) => {
            const decoded = req.decoded?.email;
            const queryEmail = req.query?.email;
            if (decoded === queryEmail) {
                const query = { email: queryEmail };
                const cursor = medicineCollection.find(query);
                const products = await cursor.toArray();
                res.send(products);
            } else {
                res.status(403).send({ message: "Forbidden Access" })
            }

        })

error picture

Progman
  • 16,827
  • 6
  • 33
  • 48
  • It looks like that something else is triggering this behavior, why are you making this request when the `user.email` changes? Please, give some context about it and include the code of the component as well. – Yago Biermann May 01 '22 at 03:54
  • In React 18 useEffect is rendering twice, even If I empty the dependency array of the useEffect hook. – Mehedy Hasan Linkon May 01 '22 at 04:10
  • Take a look at [this question](https://stackoverflow.com/questions/60618844/react-hooks-useeffect-is-called-twice-even-if-an-empty-array-is-used-as-an-ar), this problem seems to be caused by the strict mode. – Yago Biermann May 01 '22 at 04:19

1 Answers1

0

Maybe you take user.email in a state which is updating somehow so that's why useEffect is calling again and giving you twice response.