Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql NestJS中具有TypeORM的冠词和标记的关系_Sql_Relationship_Nestjs_Typeorm - Fatal编程技术网

Sql NestJS中具有TypeORM的冠词和标记的关系

Sql NestJS中具有TypeORM的冠词和标记的关系,sql,relationship,nestjs,typeorm,Sql,Relationship,Nestjs,Typeorm,实际上,我正在创建一个带有相关标签的文章系统,方法是创建一个带有文章ID和标签ID的关系表 问题是这个关系是为第一个标签而不是其他标签建立的,我不知道如何做到这一点 首先,我在文章createdto中将标签作为数组接收 import { ApiProperty } from '@nestjs/swagger'; import { IsNotEmpty } from 'class-validator'; export class CreateArticleDto { @ApiProper

实际上,我正在创建一个带有相关标签的文章系统,方法是创建一个带有文章ID和标签ID的关系表

问题是这个关系是为第一个标签而不是其他标签建立的,我不知道如何做到这一点

首先,我在文章createdto中将标签作为数组接收

import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty } from 'class-validator';

export class CreateArticleDto {
    @ApiProperty()
    @IsNotEmpty()
    title: string;

    @ApiProperty()
    @IsNotEmpty()
    content: string;

    @ApiProperty()
    author: string;

    @ApiProperty()
    tags: Array<number>;

    @ApiProperty({ type: 'string', format: 'binary' })
    image: string;
}
此外,这是我在创建文章时为文章提供的服务

async createArticle(articleDto: CreateArticleDto) {
    const readingTime = Utils.estimateReadingTime(articleDto.content);
    const slug: string = Utils.slugify(articleDto.title);
    const tags: TagsEntity[] = await this.tagsRepository.findByIds(articleDto.tags);
    const articleEntity: ArticlesEntity = await this.articlesRepository.create({
      ...articleDto,
      readingTime,
      tags,
      slug,
    });

    return this.articlesRepository.save(articleEntity);
  }

结果是,如果我尝试创建一篇包含3个标记(id 1、id 2、id 3)的文章,那么只有包含标记id 1的文章id 1,而不是包含标记id 2和3的文章id 1,而不是包含每个标记id的文章id 1三次。

直到您可以共享服务的代码,我尝试使用您使用的相同实体和数据,添加了所有标记,没有任何问题:以下是我的创建函数:

async  create(createArticleDto: CreateArticleDto) {
 const article = new Article();
 article.title = createArticleDto.title;
 ...
 const tags: Tag[] = await this.tagRepository.findByIds(createArticleDto.tags);
   article.tags = tags;
return await this.articleRepository.save(article);
}

更新

注释后,问题在于发送给Ws-Server的数组的格式 我想你是这样发送数组的[标签]:1,2,3

相反,您应该采用以下方式发送阵列:

tags[] : 1
tags[] : 2
tags[] : 4

您能分享服务代码吗?你是如何用标签存储文章的?我把它添加到了post@Youbay你的代码似乎工作正常,我担心标签数组不是从源代码中获得的,你能告诉我后端从你的前端或邮递员那里获得的数组格式吗?我正在通过发送一个ID数组(数字)来使用swagger与现有标记相对应,您将
articleDto.tags
记录到日志中并共享结果?我需要发送一个包含所有ID的唯一数组,而不是一个包含ID的对象数组。感谢您接受此回答,这不起作用,你给了我一个包含ID的对象数组的解决方案,但是我需要发送一个ID数组作为数字(int)我想你不理解语法,正如你在这里看到的
tags[]:1 tags[]:2 tags[]:4
这只是发送一个简单数组的一种方式,我将其发送到测试,这都是关于syntax的祝你有一天愉快
tags[] : 1
tags[] : 2
tags[] : 4