Currently, I am in the process of setting up a follower/following system. However, as I attempt to add a new user to the following list, I encounter an error stating
Cannot read property 'push' of undefined
. This issue results in the creation of two separate tables, one for users following others and one for users being followed by others. I am puzzled as to why the field is not being recognized. Any assistance on this matter would be greatly appreciated.
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);
The error revolves around this line:
user.following.push(follow_user);
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined