我遇到了在续集中查询多对多关系的问题
@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