【问题标题】:Why is my Many to Many relationship field undefined?为什么我的多对多关系字段未定义?
【发布时间】:2020-04-08 15:00:12
【问题描述】:

我正在尝试创建一个关注者/关注系统,当我尝试将新用户附加到以下列表时,我收到错误 Cannot read property 'push' of undefined。这最终创建了 2 个单独的表,一个用于关注其他用户的用户,一个用于被其他用户关注的用户。不知道为什么它不拿起领域?任何帮助表示赞赏。

import { Length } from "class-validator";
import {
    Column,
    CreateDateColumn,
    Entity,
    JoinTable,
    ManyToMany,
    OneToMany,
    PrimaryColumn,
    RelationCount,
    Unique,
    UpdateDateColumn
} from "typeorm";

export class User {

    @PrimaryColumn()
    public user_id: string;

    @Column()
    public first_name: string;

    @Column()
    public last_name: string;

    @Column()
    public email: string;

    @Column()
    public phone_number: string;

    @Column()
    public username: string;

    @Column()
    @CreateDateColumn()
    public created_on: Date;

    @Column()
    @UpdateDateColumn()
    public updated_at: Date;

    @ManyToMany((type) => User, (user) => user.following)
    @JoinTable()
    public followers: User[];

    @ManyToMany((type) => User, (user) => user.followers)
    @JoinTable()
    public following: User[];

    @RelationCount((user: User) => user.followers)
    public followers_count: number;

    @RelationCount((user: User) => user.following)
    public following_count: number;
}

const { user_id, 
        follow_user_id } = req.
const user_repo = getRepository(User);
const user = await user_repo.findOne({
    where: {user_id}
});
const follow_user = new User();

follow_user.user_id = follow_user_id;
user.following.push(follow_user);
const result = user_repo.save(user);

错误是指这一行user.following.push(follow_user);

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined

【问题讨论】:

    标签: arrays typescript typeorm


    【解决方案1】:

    我们使用这种方法来避免未定义的列表:

    @ManyToMany((type) => User, (user) => user.following)
    @JoinTable()
    private _followers: User[];
    

    ...

    get followers() : User[] {
      if (!_followers) {
        _followers = [];
      }
      return _followers;
    }
    

    【讨论】:

      【解决方案2】:

      我在 OneToMany 和 ManyToOne 关系中遇到了类似的错误,其中相对返回 null/undefined。

      我正在使用的解决方法是将它放在 User 类中:

        @AfterLoad()
        async nullChecks() {
          if (!this.followers) {
            this.followers = []
          }
      
          if (!this.following) {
            this.following = []
          }
        }
      

      documentation

      【讨论】:

      • 这适用于获取现有条目,但不适用于使用new MyClass() 创建新条目。
      【解决方案3】:

      我没有测试以下方法,但认为其中一种方法应该可以帮助您。

      第一种方式。在你的 User 课堂上。

          // Source code omission
          @ManyToMany((type) => User, (user) => user.followers)
          @JoinTable()
          public following: User[] = []; // ★ Added assign
          // Source code omission
      

      第二种方式。在你的 User 课堂上。

      export class User {
          // Source code omission
          constructor() { // ★ Added line
              this.following = []; // ★ Added line
          } // ★ Added line
      }
      

      第三种方式。在你使用User类的地方。

      const follow_user = new User();
      
      follow_user.user_id = follow_user_id;
      user.following = []; // ★ Added line
      user.following.push(follow_user);
      const result = user_repo.save(user);
      

      第四条路。在你使用User类的地方。

      const follow_user = new User();
      
      follow_user.user_id = follow_user_id;
      user.following = [follow_user]; // ★ Edited line
      const result = user_repo.save(user);
      

      【讨论】:

      • 第一种方法无效我收到错误Error: Array initializations are not allowed in entity relations. Please remove array initialization (= [])。第二个选项导致相同的结果。第三个选项确实有效。
      • 也许你想要更多的第四种方式。
      猜你喜欢
      • 2020-08-27
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多