What type of return function should I use in Node Express? I have come across information suggesting that I can use either any or void. However, using void is not recommended in this case. Can anyone provide some suggestions on the best approach? Currently, I am considering using
Promise<Response<any>>
, although it requires me to disable the rule "@typescript-eslint/no-explicit-any", which I am hesitant to do.
//Here: Promise<Response<any>>
export default async function registration(req: TreqBodyReg, res: Response) {
try {
const { email, password } = req.body;
const candidate = await ModelUser.findOne({ email }) as TreqBodyReg;
if (candidate) {
return res.status(400).json({ message: `There is already a user with the email ${email}` });
}
const hashPassword = bcrypt.hashSync(password, 7);
const user = new ModelUser({ email, password: hashPassword });
await user.save();
return res.status(200).json({ message: `The user with the email ${email} has been successfully registered` });
} catch (err) {
console.log(err);
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
return res.status(400).json({ message: `An error occurred during registration: ${err}` });
}
};