Hi I am trying to check for invalid cases before executing a database action. For some reason my code will run reject('Invalid E-mail address.');
but then it will also continue to run the await User.findOne({email})
line. There are several ways I can rewrite this but I would like to keep the current structure of this code. How can I terminate the function after reject and prevent the rest of the code from running? Thanks
user.database.ts
export const registerUser = async (email: string, password: string): Promise<User> => {
return new Promise(async (resolve, reject) => {
// check if email or password are null
if (!email || !password) {
reject('Invalid E-mail address or Password.');
}
// check if email is invalid
if (!EmailValidator.validate(email)) {
reject('Invalid E-mail address.');
}
// check if database already contains E-mail address
await User.findOne({email}).then((user: any) => {
if (user) {
reject('E-mail address already in use.'); // code should stop here
}
});
// anything below should not run
// create user object
const user = new User({
_id: new mongoose.Types.ObjectId(),
email: email,
password: password
});
// save user object to database
await user.save().then((result: any) => {
resolve(result);
}).catch((error: any) => {
reject(error);
});
});
};
auth.controller.ts
export const register = (req: Request, res: Response) => {
const email = req.body.email;
const password = req.body.password;
registerUser(email, password).then((user: User) => {
res.status(200).json(user);
}).catch((error: any) => {
res.status(300).json(error);
});
};