【问题标题】:I am getting always TypeError: Cannot read property 'userService' of undefined我总是收到 TypeError: Cannot read property 'userService' of undefined
【发布时间】:2021-10-06 03:01:42
【问题描述】:

我该如何解决这个错误?在下面我的代码

IUserRepository 和 UserRepository:

    import User from "./entity/User";

export default interface IUserRepository{
    findAll():Promise<User[]>
}
import User from "./entity/User";
import IUserRepository from './IUserRepo';

    export default class UserRepository implements IUserRepository{
        
        public async findAll(): Promise<User[]>{
            return await User.findAll()
        }

}

IUserService 和 UserService:

import User from "./entity/User";

export default interface IUserRepository{
    findAll():Promise<User[]>
}

import User from './entity/User';
import IUserService from './IUserService';
import IUserRepository from './IUserRepo';
export default class UserService implements IUserService{

    public userRepo: IUserRepository
    public constructor(userRepo:IUserRepository){
        this.userRepo = userRepo
    }

    public async findAll(): Promise<User[]> {
        return await this.userRepo.findAll()
    }
    
}

用户控制器:

import { Request, Response } from 'express';
import IUserService from './IUserService';

export default class UserController{
    public userService:IUserService
    public constructor(UserService:IUserService){
        this.userService = UserService
    }

    public async findAll(req:Request, res:Response){
        const allUser = await this.userService.findAll()
        res.send(allUser)
    }
}

服务器:

import express, { Application, Request, Response } from 'express'
import UserController from './UserController';
import UserService from './UserService';
import UserRepository from './UserRepository';

const app: Application = express()
const port = 3000
const userCont = new UserController(new UserService(new UserRepository()))
app.use(express.json());
app.get('/', userCont.findAll)


    app.listen(port, () => {
        console.log(`Server running on http://localhost:${port}`)
    })

错误:

(node:21576) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'userService' of undefined
    at C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:11:36
    at step (C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:33:23)      
    at Object.next (C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:14:53)
    at C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:4:12)  
    at UserController.findAll (C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:44:16)
    at Layer.handle [as handle_request] (C:\Users\hasan\OneDrive\Masaüstü\type-orm\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\hasan\OneDrive\Masaüstü\type-orm\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\hasan\OneDrive\Masaüstü\type-orm\node_modules\express\lib\router\route.js:112:3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:21576) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21576) [DEP0018] 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.
(node:21576) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'userService' of undefined
    at C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:11:36
    at step (C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:33:23)      
    at Object.next (C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:14:53)
    at C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:4:12)  
    at UserController.findAll (C:\Users\hasan\OneDrive\Masaüstü\type-orm\src\UserController.ts:44:16)
    at Layer.handle [as handle_request] (C:\Users\hasan\OneDrive\Masaüstü\type-orm\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\hasan\OneDrive\Masaüstü\type-orm\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\hasan\OneDrive\Masaüstü\type-orm\node_modules\express\lib\router\route.js:112:3)
(node:21576) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

我已经收到这个错误 4 天了,我会因为这个错误而生气。请帮助meeee。 顺便说一句,对不起我的英语不好。如果我写得少,我不能分享这篇文章,因为对这个 xxx 垃圾邮件感到抱歉 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

【问题讨论】:

    标签: node.js typescript oop dependency-injection sequelize.js


    【解决方案1】:

    你不应该传递对象的函数,因为你失去了上下文

    改用箭头函数:

    app.get('/', (req, res) =&gt; userCont.findAll(req,res))

    【讨论】:

    • 谢谢你 Ayder 你救了我的命 :) 我不再生气了
    【解决方案2】:

    你能发布堆栈跟踪或错误吗?我想你忘了说。

    【讨论】:

    • 我在@yed2393 上方添加了堆栈跟踪
    猜你喜欢
    • 2023-02-22
    • 2021-10-20
    • 1970-01-01
    • 2019-01-11
    • 2020-09-20
    • 2021-06-25
    • 2021-09-25
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多