如何修复此错误?在下面的代码中
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()
}
}
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)
}
}
服务器:
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天了,我会因为这个错误而发疯的。请帮帮我。顺便说一句,很抱歉我的英语不好。如果我写得太少,我就不能分享这篇文章,因为这篇文章是关于xxx垃圾邮件的。
发布于 2021-10-05 11:01:42
不应该传递object的函数,因为这样会使上下文松散
改用箭头函数:
app.get('/', (req, res) => userCont.findAll(req,res))
发布于 2021-10-05 04:41:41
你能发布堆栈跟踪或错误吗?我想你忘了写了。
https://stackoverflow.com/questions/69449598
复制