【问题标题】:How to fix this Implementation that doesn't work?如何修复这个不起作用的实现?
【发布时间】:2022-08-16 02:07:29
【问题描述】:

用户存储库实现了 IUsersRepository,尽管我无法从 IUsersRepository 获取值

The error that is shown

UserRepository 实现 IUserRepository 接口

import { getRepository, Repository } from \"typeorm\"; 
import { IUsersRepository } from \"../IUsersRepository\"; 
import { User } from \"../../entities/User\";
import { ICreateUserDTO } from \"../../../../dtos/usersDto\";

class UsersRepository implements IUsersRepository {

    private repository: Repository<User>;
    
    constructor() {
        this.repository = getRepository (User);
    }

    async create({ name, email, password, age }: ICreateUserDto): Promise<void> {
        const user = this.repository.create({
            name,
            email, 
            password,
            age
        }); 
        await this.repository.save(user);
    }

    async findBy Email(email: string): Promise<User> {
        const user = this.repository.findByEmail({ email }); 
        return user;
    }

    async findById(id: string): Promise<User> {
        const user = this.repository.findById(id); 
        return user;
    }
    
export { Users Repository }

IUserRepository 接口:

import { ICreateUserDTO } from \"../../../dtos/usersDto\", 
import { User } from \"../entities/User\";

interface IUsersRepository {
    create (data: ICreateUserDTO): Promise<void>; 
    findByEmail(email: string): Promise<User>; 
    findById(id: string): Promise<User>;
}

export { IUsersRepository }

我该如何解决?

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如它目前所写的那样,很难准确地说出你在问什么。

标签: node.js typescript typeorm


【解决方案1】:

findByEmail 不是 typeorm 的 Repository 类的函数。 它甚至在您的屏幕截图中标有红线。 您需要在那里编写正确的查询!

【讨论】:

  • 但是我使用了 findByEmail 和 findById,因为我正在实现 IUsersRepository 接口。
  • 请将您的代码发布为代码而不是屏幕截图。你的类有成员变量private repository: Repository&lt;User&gt;,你正在调用this.repository.findByEmail({email})。就像我说的来自 typeorm 的Repository 没有这样的方法
猜你喜欢
  • 2021-01-27
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 2016-11-22
  • 2022-01-12
  • 2011-01-12
  • 2013-12-24
  • 2020-04-02
相关资源
最近更新 更多