Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda