quiero obtener el valor de esta variable fuera de la función const custDetail = await registerUser.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), }); } };Edite una forma simple de pasar datos a otra función usando res.locals
enrutador:
router.get('getDashboardRef/:mobile', userCtl.dashboardReq, userCtl.nextFunction)tableroReq
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, }); } };función siguiente
const nextFunction = (req, res) => { //do stuff with res.locals.custDetail res.status(201).json({ status: "success", data: { res.locals.regCust }, }); }intente revisar su diseño ya que la variable custDetail debe existir dentro de la ruta misma
si se supone que esto se debe memorizar, mantenga un mapa con algún identificador único de req.params con el resultado de registerUser.findOne