【发布时间】: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