Home > front end >  I am getting always TypeError: Cannot read property 'userService' of undefined
I am getting always TypeError: Cannot read property 'userService' of undefined

Time:10-06

How can i fix this error ? In below my codes

IUserRepository and 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 and 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()
    }
    
}

UserController:

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

server:

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

error:

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

I have got this error for 4 day, i will become mad becouse of this error. Please help meeee. btw sorry for my bad english. if i write less i cant share this post becouse of sory for this xxx spam xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

CodePudding user response:

Could you post the stack trace or error? I think you forgot to put that.

CodePudding user response:

You should not pass a function of object, because you loose context

Use arrow function instead:

app.get('/', (req, res) => userCont.findAll(req,res))

  • Related