Я сталкиваюсь с проблемами, связанными с отношением многие ко многим в sequelize
@Table
export class Video extends Model<Video> {
@Unique
@PrimaryKey
@AutoIncrement
@Column
id!: number;
@Column
imageUrl!: string;
@Column
url!: string;
@Column
title!: string;
@Column
description!: string;
@BelongsToMany(() => Tag, () => VideoTag)
tags?: Tag[];
}
@Table
export class Tag extends Model<Tag> {
@Unique
@PrimaryKey
@AutoIncrement
@Column
id!: number;
@Column
title!: string;
@BelongsToMany(() => Video, () => VideoTag)
videos?: Video[];
}
@Table
export class VideoTag extends Model<VideoTag> {
@ForeignKey(() => Video)
@Column
videoId!: number;
@ForeignKey(() => Tag)
@Column
tagId!: number;
}
Учитывая строку запроса, я хочу вернуть список видео, где queryString является подстрокой либо video.title
или video.tags[*].title
Это то, что у меня сейчас, но не работает
export function searchVideo(req: any, res: any, next: any) {
if (req.query.q) {
const queryString = req.query.q.toLowerCase();
Video.findAll({
limit:10,
include: [{
model: Tag
}],
where: {
[Op.or]: [
{
title: {
[Op.like]: `%${queryString}%`
}
},
{
tags: [ {
title: {
[Op.like]: `%${queryString}%`
}
}]
}
]
}
})
.then((videos) => {
res.send(videos)
})
.catch(err => {
console.log(err);
res.setHeader('Content-Type', 'application/json');
res.statusCode = 400;
res.send(`{"errorMessage": "${err}"}`)
})
} else {
res.redirect('/videos');
}
}
Я получаю Error: Invalid value { title: { [Symbol(like)]: '%j%' } }
Может кто-нибудь, пожалуйста, помогите? asdasdas