Im using the async function inside an object to send a response in express.js
Controller Code:
module.exports = { async signUpEmail(req, res) { const firstName = req.body.firstName; res.send({ success: name }); throw new Error();
Question:
Since the signUpEmail method is asynchronous in my case, and it will be rejected with any of my async methods, it returns Error . (intentionally placed)
so go to the console.
(node:13537) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error (node:13537) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
So, I have to process it from the routes where I call it from.
Router Code
const routes = require('express').Router(); const SignUpController = require('../controllers/signUpController')
something like this SignUpController.signUpEmail().then(…); But since I do not call the function in the routes that I just go through. How can this be done effectively ?
PS: Please do not offer too complicated solutions. I start with JS and study.
I did not use attached route handlers because I want to create a modular, mounted route.
Official Doc Example
source share