Node.js 如何通过多对多关系实体id查找实体

Node.js 如何通过多对多关系实体id查找实体,node.js,typeorm,Node.js,Typeorm,我有多对多链接的标签和文章实体。 如果我知道物品id,如何找到标签 @Entity("articles") class ArticleEntity { @ManyToMany(() => TagEntity, tag => tag.articles) @JoinTable() tags: TagEntity[]; } @Entity("tags") class TagEntity { @ManyToMany(() =>

我有多对多链接的标签和文章实体。 如果我知道物品id,如何找到标签

@Entity("articles")
class ArticleEntity {
  @ManyToMany(() => TagEntity, tag => tag.articles)
  @JoinTable()
  tags: TagEntity[];
}

@Entity("tags")
class TagEntity {
  @ManyToMany(() => ArticleEntity, article => article.tags)
  articles: ArticleEntity[];
}

const articleId = 1;
this.tagRepo.find({ where: { articles: { id: articleId } } }); // ??
  • 要加载带有标记的文章,必须在
    FindOptions
  • 使用
    QueryBuilder
    您可以加入它们

  • 仅供参考:彻底回答问题非常耗时。如果你的问题得到了解决,那就接受最符合你需要的解决方案来表示感谢。接受检查位于答案左上角的向上/向下箭头下方。如果出现更好的解决方案,则可以接受新的解决方案。如果你有15+的声誉,你也可以用向上或向下箭头对答案的质量/帮助性进行投票。如果解决方案不能回答问题,请留下评论。谢谢。通过添加解释和更多细节,您提供了质量更好的答案。这一点得到了社会的赞赏。
    const articleId = 1;
    const article = await connection.getRepository(ArticleEntity)
                 .findOne(articleId, {relations: ["tags"]});
    
    console.log(article.tags); // contains all tags related to the article.id = 1
    
    const article = await connection
        .getRepository(ArticleEntity)
        .createQueryBuilder("articles")
        .leftJoinAndSelect("articles.tags", "tags")
        .where({ id: articleId })
        .getMany();
    
    console.log(article.tags);