Hello,I have a route setup in my Express app that uses this function, lets call this function A:module.exports.getUserEmail = async (req, res) => { const user = await util.getCurrentUser(req, res); console.log(user); }; The function that is being called by util.getCurrentUser(req, res) is this function:module.exports.getCurrentUser = async (req, res) => { const token = req.cookies.JWT; if (token) { jwt.verify(token, process.env.JWT_SECRET, async (err, decodedToken) => { if (err) { console.log(err.message()); return null; } else { const user = await User.findById(decodedToken.id); return user; } }); } else { return null; } } Now in the first function the console.log(user) returns undefined, however in the second function the console.log(user) returns the expected result. Why is the user in the first function undefined?
Submitted September 20, 2020 at 10:08PM by rubennebur
No comments:
Post a Comment