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

153
Vistas
how can i get the value of a variable outside the arrow function in mern stack

i want to get the value of this variable outside the function const custDetail = await registeredUser.findOne(req.params);

const dashboardReq = async (req, res, next) => {
      try {
        const regCust = await registeredUser.findOne({ mobile: req.params.mobile });

    if (regCust == null) {
      console.log("No user found");
    } else {
      const custDetail = await registeredUser.findOne(req.params);
    }
    res.status(201).json({
      status: "success",
      data: { regCust },
    });
  } catch (error) {
    res.status(400).json({
      status: "fail",
      data: next(error),
    });
  }
};
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Edit a simple way to pass data to another function using res.locals

router:

router.get('getDashboardRef/:mobile', userCtl.dashboardReq, userCtl.nextFunction)

dashboardReq

const dashboardReq = async (req, res, next) => {
      try {
        res.locals.regCust = await registeredUser.findOne({ mobile: req.params.mobile });

    if (!res.locals.regCust) {
      console.log("No user found");
      throw new Error("No user found")
    } else {
      res.locals.custDetail = await registeredUser.findOne(req.params);
     next()
    }
  } catch (error) {
    res.status(400).json({
      status: "fail",
      data: error,
    });
  }
};

nextFunction

const nextFunction = (req, res) => {
  //do stuff with res.locals.custDetail
 res.status(201).json({
      status: "success",
      data: { res.locals.regCust },
    });
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

try to revisit your design since the variable custDetail needs to exist inside the route itself

if this is supposed to be memoized, then maintain a map with some unique identifier from req.params with the result from registeredUser.findOne

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