Im using the async function inside the object to send a response in express.js
Controller Code :
module.exports = {
async signUpEmail(req, res) {
/**
* @description Parameters from body
* @param {string} firstName - First Name
* @inner
*/
const firstName = req.body.firstName;
res.send({ success: name });
throw new Error(); // purposely Done
}
}
Question:
Since the signUpEmail method is async in my case and it will get rejected with whatever my async method throw's here it's comes Error
.(purposely put there)
so getting this logged in 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'm supposed to handle it from the routes from where i'm calling it.
Router Code
const routes = require('express').Router();
const SignUpController = require('../controllers/signUpController')
// /signup
routes.post('/', SignUpController.signUpEmail);
module.exports = routes;
some what like this SignUpController.signUpEmail().then(…);
But since i'm not calling function in the routes i'm just passing. How this can be done effectively ?
PS:Please Don't suggest too complicated solutions. I'm beginner with JS and is learning through.
I Didn't use chainable route handlers because i want to create modular, mountable route handler.
routes.pos('/', (...args) => SignUpController.signUpEmail().then(…))
– Himyaritic…signUpEmail(...args)…
or whatever your function expects – HimyariticReferenceError: args is not defined
would be very much helpful – Brislingroutes.post('/', (req, res) => SignUpController.signUpEmail(req, res).then(…))
– HimyariticSignUpController.signUpEmail(req, res)
and making the result as returned by normal function .. Correct me if i didn't understand.. Thanks for your great help :) – Brislingasync/await
is not part of ES7, it's part of ES2017. – Pithos