I try to get the data from a function by using variables. first I declare a variable empty and then inside the function, I store some data from the DB in a variable. try-catch sending the data properly but when I console the variable after the function then it's showing undefined.
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");
}
custDetail = await registeredUser.findOne(req.params);
res.status(201).json({
status: "success",
data: {
custDetail
},
});
} catch (error) {
res.status(400).json({
status: "fail",
data: next(error),
});
}
};
console.log(custDetail);