I created an email checker function to verify if the email exists before registering a new user. I assigned req.body to user. However, the newUser function within my email checker function is not recognizing the variable user. How can I pass user into the function so if the email is not recognizing I can create the new user?
Here is my code:
app.post("/api/user", function(req, res) {
var user = req.body;
console.log("line 117", user);
var searchUser = {
email: user.email
};
User.findOne(searchUser, function(err, user){
if (err) throw err;
if (user) return res.status(401).send({
message: "email already exists"
});
var newUser = new User({
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
password: user.password
});
newUser.save(function(err) {
createSendToken(newUser, res);
});
});
});