gpt4 book ai didi

javascript - typeORM 黑名单用户

转载 作者:行者123 更新时间:2023-11-30 19:40:43 25 4
gpt4 key购买 nike

在我的社交应用程序中,用户可能想要将某个用户列入黑名单(就像很多社交应用程序一样)。

我有 2 个实体:

用户 (但已列入黑名单的用户) ManyToOne 黑名单

用户 多对多 黑名单

用户:

@Entity()
@Unique(["username"])
export class User
{
@PrimaryGeneratedColumn()
id: number;

@Column()
@Length(4, 20)
username: string;

@Column()
@Length(4, 100)
password: string;

@Column()
@IsNotEmpty()
role: string;

@Column()
@CreateDateColumn()
createdAt: Date;

@OneToMany(type => Blacklist, blacklist => blacklist.user)
blacklist: Blacklist[];

@ManyToMany(type => Blacklist)
@JoinTable({
"name": "user_blacklist"
})
blacklists:Blacklist[]

@Column()
@UpdateDateColumn()
updatedAt: Date;

hashPassword() {
this.password = bcrypt.hashSync(this.password);
}

checkIfUnencryptedPasswordIsValid(unencryptedPassword: string) {
return bcrypt.compareSync(unencryptedPassword, this.password);
}
}

黑名单:

@Entity()
export class Blacklist {
@PrimaryGeneratedColumn()
id: number;

@ManyToOne(type => User, user => user.blacklist)
user: User;
}

这是我在 post('user/blacklist')

上调用的方法
static addBlacklist = async (req: Request, res: Response) => {
const { id, blacklisted_id } = req.body;

const blacklistRepository = getRepository(Blacklist);
const userRepository = getRepository(User);

let user: User
let blacklistedUser: User;
try {
user = await userRepository.findOneOrFail(id);
blacklistedUser = await userRepository.findOneOrFail(blacklisted_id);
} catch(error) {
res.status(404).send({error: error});
}
let blacklist = new Blacklist();
blacklist.user = blacklistedUser;
// user.blacklists.push(blacklist);

try {
await blacklistRepository.save(blacklist);
await userRepository.save(user);
res.status(201).send('success blacklisted');
} catch (error) {
res.status(404).send({error: error});
}
}

它适用于黑名单,我有一个新的黑名单,其中包含黑名单用户的 ID。但是我不能添加 user_blacklist(希望我清楚)

所以我尝试添加:

user.blacklists.push(blacklist); // see above comment

但是我明白了

(node:40495) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined

实际:

blacklist // works
+------------+--------------+
| id | userId |
+------------+--------------+
| 1 | 1 |
+------------+--------------+
user_blacklist // not works cause of cannot push and save
+------------+--------------+
| userId | blacklistId |
+------------+--------------+
| Empty |
+------------+--------------+

预期:(因此用户 #2 被列入用户 #1 的黑名单)

blacklist
+------------+--------------+
| id | userId |
+------------+--------------+
| 1 | 2 |
+------------+--------------+
user_blacklist
+------------+--------------+
| userId | blacklistId |
+------------+--------------+
| 1 | 1 |
+------------+--------------+

感谢帮助

最佳答案

你需要做的:

user.blacklists = [blacklist];

然后保存用户实体。

有关更多信息,请查看此处 https://github.com/typeorm/typeorm/blob/master/docs/many-to-many-relations.md

关于javascript - typeORM 黑名单用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55433450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com