【问题标题】:.then() is not function in node.js.then() 在 node.js 中不起作用
【发布时间】:2021-04-03 22:31:45
【问题描述】:

我是 node.js 的新手。我尝试使用 express 来自定义有效负载和响应。但是当我调用 api 时,它显示 .then() 不是函数

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)
      })
    }

}

我得到的错误是

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)

路线在下方

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)
}

我已更新问题。这是我的控制器层 出了什么问题。我试过了还是找不到这个

谢谢提前!!

【问题讨论】:

  • SignUp 是在哪里定义的? .then 只能应用于承诺。注册可能没有返回承诺
  • 请将代码粘贴到'../../Config/ioc'中

标签: node.js typescript express


【解决方案1】:

SignUp 函数应该返回承诺。像这样向你的函数添加承诺

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)
}

【讨论】:

  • 这是一个非常糟糕的做法。使用 return await 在异步函数中毫无意义,并且会减慢您的代码速度(增加了一层额外的 Promise 来解决)。另外,如果之前没有返回一个promise,那么SignUp就不是异步的,那await是没有意义的
猜你喜欢
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 2016-02-09
  • 2020-09-04
  • 2019-06-25
  • 1970-01-01
  • 2016-01-17
相关资源
最近更新 更多