Reputation: 23
I'm new to node.js. I trying to use express to customize the payload and response. But when I calling the api it shows .then() is not a function
ExpressHandler.ts
import httpStatus from 'http-status'
export const ExpressHandler = (controller) =>{
return (req, res, next)=>{
const httpRequest = {
body: req.body,
query: req.query,
params: req.params,
ip: req.ip,
method: req.method,
files: req.files,
file: req.file,
path: req.path,
headers: {
'Content-Type': req.get('Content-Type'),
Referer: req.get('referer'),
'User-Agent': req.get('User-Agent')
}
}
controller(httpRequest)
.then((responseObj) => {
return res.send(responseObj)
})
.catch((err) => {
console.log('>>>>ERROR>>>>\n\n\n\n\n',err,'\n\n\n\n\n<<<<<ERROR<<<<<')
const errorMessage =
typeof err.errorResponse !== 'undefined' ? err.errorResponse : err
console.log('<<<ruleServiceConfig>>>==>\n\n\n\n',errorMessage,'\n\n\n\n<<<ruleServiceConfig>>>==>')
const apiErr = new Error(
errorMessage,
// typeof err.resType !== 'undefined' ? err.resType : 'error',
// typeof err.status !== 'undefined'
// ? err.status
// : httpStatus.INTERNAL_SERVER_ERROR,
// typeof err.isPublic !== 'undefined' ? err.isPublic : true
)
next(apiErr)
})
}
}
Error I got is
TypeError: controller(...).then is not a function
at ExpressHandler (D:\Learning\NodeSign\src\Config\ExpressHandler\index.ts:24:10)
at Layer.handle [as handle_request] (D:\Learning\NodeSign\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Learning\NodeSign\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Learning\NodeSign\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Learning\NodeSign\node_modules\express\lib\router\layer.js:95:5)
at D:\Learning\NodeSign\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\Learning\NodeSign\node_modules\express\lib\router\index.js:335:12)
at next (D:\Learning\NodeSign\node_modules\express\lib\router\index.js:275:10)
at expressInit (D:\Learning\NodeSign\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (D:\Learning\NodeSign\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\Learning\NodeSign\node_modules\express\lib\router\index.js:317:13)
at D:\Learning\NodeSign\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\Learning\NodeSign\node_modules\express\lib\router\index.js:335:12)
at next (D:\Learning\NodeSign\node_modules\express\lib\router\index.js:275:10)
at query (D:\Learning\NodeSign\node_modules\express\lib\middleware\query.js:45:5)
at Layer.handle [as handle_request] (D:\Learning\NodeSign\node_modules\express\lib\router\layer.js:95:5)
route is below
import express from 'express'
import {ExpressHandler} from '../Config/ExpressHandler'
import SignUp from '../Controllers/User/SignUp'
const UserRoute = express()
UserRoute.get("/SignUp",ExpressHandler(SignUp))
export default UserRoute
import {UserContainer} from '../../Config/ioc'
import {IUserService} from '../../Service/User/IUserService'
export const SignUp = (httpRequest) => {
const userService = UserContainer.get<IUserService>(
Symbol.for('IUserService')
)
return userService.SignUp(httpRequest.body)
}
I have updated the question. This is my controller layer what's going wrong with is . I tried still not able find out this
Thanks advance !!
Upvotes: 2
Views: 83
Reputation: 641
SignUp function should return the promise. Add promise to your function like so
import {UserContainer} from '../../Config/ioc'
import {IUserService} from '../../Service/User/IUserService'
export const SignUp = async (httpRequest) => {
const userService = UserContainer.get<IUserService>(
Symbol.for('IUserService')
)
return await userService.SignUp(httpRequest.body)
}
Upvotes: 2