Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

676
Vistas
Failed to load resource: the server responded with a status of 401 (Unauthorized) in REACT and NODE.js

This is REACT js client side code in console error is this 1.Failed to load resource: the server responded with a status of 401 (Unauthorized) 2. Uncaught TypeError: inventories.map is not a function

 useEffect(() => {
        setDataLoading(true);
        const url = `http://localhost:5000/inventoryByEmail?email=${user.email}`;
        fetch(url, {
          headers: {
            authorization: `Bearer ${localStorage.getItem("access-token")}`,
          },
        })
          .then((res) => res.json())
          .then((data) => {
            setInventories(data);
            setDataLoading(false);
          });
      }, [user]);

This is Server site Nodejs code

function verifyJWT(req, res, next) {
  const authHeader = req.header.authorization;

  if (!authHeader) {
    return res.status(401).send({ message: "unauthorized access" });
  }
  const token = authHeader.split(" ")[1];
  jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
    if (err) {
      return res.status(403).send({ message: "Forbidden access" });
    }
    req.decoded = decoded;
    next();
  });
}

and this is api Route

app.get("/inventoryByEmail", verifyJWT, async (req, res) => {
      const decodedEmail = req.decoded.email;
      const email = req.query.email;
      if (email === decodedEmail) {
        const query = { email: email };
        const cursor = inventoryCollection.find(query);
        const result = await cursor.toArray();
        res.send(result);
      } else {
        res.status(403).send({ message: "forbidden access" });
      }
    });

How to slove this issue? thanks so much for your time!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

in your function verifyJWT it's : req.headers.authorization; or if you want to get the token : req.headers.authorization.split(' ')[1];

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda