首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我得到always TypeError:无法读取未定义的属性'userService‘

我得到always TypeError:无法读取未定义的属性'userService‘
EN

Stack Overflow用户
提问于 2021-10-05 03:27:59
回答 2查看 182关注 0票数 0

如何修复此错误?在下面的代码中

IUserRepository和UserRepository:

代码语言:javascript
运行
AI代码解释
复制
    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:

代码语言:javascript
运行
AI代码解释
复制
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:

代码语言:javascript
运行
AI代码解释
复制
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)
    }
}

服务器:

代码语言:javascript
运行
AI代码解释
复制
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}`)
    })

错误:

代码语言:javascript
运行
AI代码解释
复制
(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垃圾邮件的。

EN

回答 2

Stack Overflow用户

发布于 2021-10-05 11:01:42

不应该传递object的函数,因为这样会使上下文松散

改用箭头函数:

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

票数 1
EN

Stack Overflow用户

发布于 2021-10-05 04:41:41

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69449598

复制
相关文章
[Centos7]open读取文件报错:TypeError
本文编写于 205 天前,最后修改于 205 天前,其中某些信息可能已经过时。 在写对比文件差异的脚本时,运行脚本报错: TypeError: a bytes-like object is required, not ‘str’ 处理方法如下: 1.使用codecs模块 2.原代码为: fileHandle = open(filename, 'rb') #此写法用于python2.x版本,因为我的版本为python3.6,所以需要做以下更改 3.更改后的代码为 import codecs fileHandl
贰叁壹小窝
2020/07/22
1K0
Flex反射得到属性和属性的值
       今天要写一个生成json的方法,目的是将VO对象中的所有公共属性和值转换成一个json对象,这个类中20多个属性,手动拼的话,是个体力活,并且有其它的对象也要转成json,还要手动拼,脑袋里最先想到的就是反射。
高爽
2022/05/07
1.8K0
SQL SERVER ALWAYS ON 为什么日志无法dump
SQL SERVER 还有人用,对的,很多人都在用,尤其很多企业,非互联网的企业。那今天就说说 SQL SERVER ALWAYS ON 高可用集群中,为什么不切日志的问题。引起这篇文字的原因是有一个81G 都没有切除日志的 AWO集群。
AustinDatabases
2020/03/10
1.2K0
SQL SERVER  ALWAYS ON 为什么日志无法dump
opencv无法读取图片_opencv无法读取图片
使用一下代码读取一张图片失败(不管是绝对路径还是相对路径,都失败),工程运行都没问题,就是图片读取失败。
全栈程序员站长
2022/11/04
2.5K0
Java-方法重载时 调用未定义的对象属性
public class TestWayReload { int id; String name; String pwd; public TestWayReload(){ System.out.println(“Hellow World!”); System.out.println("################"); }
Fisherman渔夫
2019/07/30
6.1K0
来自1000多个项目的10大JavaScript错误浅析
出于可读性方面的考虑,每个错误的描述经过精简。 1.Uncaught TypeError: Cannot read property 如果你是一名JavaScript开发者,对这个错误可能已经熟视无睹。在Chrome里读取未定义对象的属性或调用未定义对象的方法时就会发生这个错误,在Chrome开发者控制台可以很容易地重现这个错误。 发生这个错误的原因有很多,其中最为常见的是,在渲染UI组件时没有正确初始化状态。我们通过一个真实的例子来看看这个错误是怎么发生的。我们选择React作为示例,不过在其
用户1263954
2018/03/20
6.7K0
来自1000多个项目的10大JavaScript错误浅析
我曾得到的最佳编程建议
我曾得到的最佳编程建议   这是一个有关于我得到的一些专业性编程建议的故事,以及它如何影响了我的职业生涯。我真心觉得此建议真可谓是字字珠玑,所以我想分享给大家。  建议   还是在1996年,我刚
用户1289394
2018/02/27
7800
我曾得到的最佳编程建议
Mac无法读取硬盘
问题描述: 由于没有弹出移动硬盘,就拔出来了。导致再插入硬盘,电脑也无法识别了。 步骤: 1.查看一下硬盘信息 画圈的就是我的硬盘。 2.将这个硬盘挂载 sudo diskutil mount /dev/disk2s1 然后需要输入的就是你的密码(开机密码) 可以看到,已经挂载成功了。
用户4793865
2023/01/12
1.2K0
ABC: Always Be Coding
平时有很多碎片化时间,比如下班的地铁上,或者等待的时间,我们总喜欢拿出手机玩,这个时间也可以用来学习呢,当然佳爷自己也想学习英语,所以上下班的时间看看。
仇诺伊
2020/05/25
1.7K0
Verilog 里面,always,assign和always@(*)区别
敏感事件列表中可以包含多个敏感事件,但不可以同时包括电平敏感事件和边沿敏感事件,也不可以同时包括同一个信号的上升沿和下降沿,这两个事件可以合并为一个电平敏感事件。
碎碎思
2020/12/30
4.9K0
Verilog 里面,always,assign和always@(*)区别
Verilog 里面,always,assign和always@(*)区别
1.always@后面内容是敏感变量,always@(*)里面的敏感变量为*,意思是说敏感变量由综合器根据always里面的输入变量自动添加,也就是所有变量都是敏感列表,不用自己考虑。2.如果没有@,那就是不会满足特定条件才执行,而是执行完一次后立马执行下一次,一直重复执行,比如testbench里面产生50Mhz的时钟就(假设时间尺度是1ns)可以写成
FPGA开源工作室
2022/04/02
2.1K0
logback.xml读取spring的属性
因为logback.xml和logback-test.xml会被logback组件直接读取,所以如果要交给spring管理,需要
十毛
2019/08/03
5.9K0
如何遍历一个实例的所有属性,得到属性的名称和值
College college = this.collegeService.getCollegeById(id); try { Field[] fields = college.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true);//类中的成员变量为private,须进行此操作 System.out.pri
qubianzhong
2018/09/19
2.8K0
读取项目属性文件的几种方式
项目中会把一些环境变量、公共属性配置到属性文件中,总结了一些工程加载属性文件的方式。
用户2146693
2019/08/08
1.9K0
spring-PropertyPlaceholderConfiger读取属性
spring在读取配置文件的时候,我们时常使用@Value注解来注入配置文件中的配置,在配置文件中也可以通过${}的方式来引用已经申明的配置,这是依靠Spring提供的PropertyPlaceholderConfigure来实现的。
leobhao
2022/06/28
6540
mongodb用mongoose得到的对象不能增加属性解决
上述两个models的关系可以看出:一个用户对应一个购物车(cartList),一个购物车有多个商品对象
wfaceboss
2019/04/08
1.8K0
python读取图片属性信息
从照片里面获取GPS信息。可交换图像文件常被简称为EXIF(Exchangeable image file format),是专门为数码相机的照片设定的,可以记录数码照片的属性信息和拍摄数据,EXIF信息不支持png,webp等图片格式。
全栈程序员站长
2022/10/01
2.2K0
python读取图片属性信息
vue在IE下无法正常工作,Promise未定义?
用vue写了一个日历组件,在Firefox、Edge、Chrome以及360等浏览器极速模式中运行一切正常,如图:
Yiiven
2022/12/15
4.5K0
vue在IE下无法正常工作,Promise未定义?
一些我用得到的 Python 脚本
下面的python脚本有一部分是我百度然后修改的,一部分是我自己写的,如果以后有什么新的我用的到的脚本或者这些脚本有修改我也会在这里更新,这些源码放在这里仅以备份为目的
猫猫摸大鱼
2022/07/21
7050
1000多个项目中的十大JavaScript错误以及如何避免
通过统计数据库中的1000多个项目,我们发现在 JavaScript 中最常出现的错误有10个。下面会向大家介绍这些错误发生的原因以及如何防止。
一墨编程学习
2018/10/20
6.7K0

相似问题

我得到(TypeError:无法读取未定义的属性'down‘)

13

我得到了"TypeError:无法读取未定义的属性(读: 0)“

12

我得到了错误=> TypeError:无法读取未定义属性的“图像”

24

为什么我要得到'TypeError:无法读取属性‘片’的未定义‘?

13

用火表示我得到了TypeError:无法读取未定义的属性“trim”

15
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档