简体   繁体   中英

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),
    });
  }
};

If my comment didn't make sense:

let custDetail;

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 {
      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),
    });
  }
};

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM