【问题标题】:Validation failed: Cast to Array failed for value验证失败:转换为数组的值失败
【发布时间】:2020-07-22 15:36:37
【问题描述】:

我正在尝试将 Typegoose 与 GrqphQL、MongoDB 和 Nest.js 一起使用。我想创建一个可以创建帖子的突变。我为简单的Post 创建了模型、服务和解析器。当我尝试运行我的变异来创建一个帖子时,我收到了这个错误:

PostModel 验证失败:部分:Cast to Array 值失败:

[
  [Object: null prototype] {
    title: 'section 1',
    body: [ 'section test lalal' ],
    code: [ "console.log('hello world!)" ],
    image: [ 'kanow.svg' ]
  }
]

在路径“部分”

我不知道为什么会收到我尝试使用的此错误:refitemsRefs(用于类和字符串值。您可以在此处阅读有关此内容的更多信息: typegoose arrayProp)。在我创建新的PostModelconsole.log 每个属性之后,我可以看到sections 是一个空数组,但它不应该,因为在postInput 中我可以找到这个JSON:

[Object: null prototype] {
  title: 'refactored post',
  image: 'rest.jpg',
  tags: [ 'ref, ref' ],
  sections: [
    [Object: null prototype] {
      title: 'section 1',
      body: [Array],
      code: [Array],
      image: [Array]
    }
  ]
}

我认为这个 json 看起来不错,所以它不应该是这个错误的来源。

我想知道我做错了什么以及为什么它不起作用。下面我附上了一些代码。如果您需要其他任何内容,请在 cmets 中告诉我。

GraphQL 突变

mutation {
  createPost(postInput: {
    image: "rest.jpg",
    title: "refactored post"
    tags: ["ref, ref"]
    sections: [{
      title: "section 1"
      body: ["section test lalal"]
      code: ["console.log('hello world!)"]
      image: ["kanow.svg"]
    }]
  }) {
    _id
    title
    tags
    sections {
      title
      body
      code
      image
    }
  }
}

post.service.ts

@Injectable()
export class PostsService {
    constructor(@InjectModel(PostModel) private readonly postModel: ReturnModelType<typeof PostModel>) {
    }

    async create(postInput: CreatePostInput): Promise<DocumentType<PostModel>> {
        const createdPost: DocumentType<PostModel> = new this.postModel(postInput);
        return await createdPost.save();
    }
 ...
}

post.model.ts

@ObjectType()
export class PostModel {
    @Field(() => ID)
    readonly _id: ObjectId;

    @Field()
    @prop({required: true})
    title: string;

    @Field()
    @prop({nullable: true})
    image: string;

    @Field(() => [String])
    @arrayProp({items: String})
    tags: string[];

    @Field(() => [SectionModel])
    @arrayProp({ref: 'SectionModel'})
    sections: Ref<SectionModel>[];
}

section.model.ts

@ObjectType()
export class SectionModel {
  @Field()
  @prop({ required: true })
  title: string;

  @Field(() => [String])
  @arrayProp({ items: String })
  body: string[];

  @Field(() => [String])
  @arrayProp({ items: String })
  code: string[];

  @Field(() => [String])
  @arrayProp({ items: String })
  image: string[];
}

create-post.input.ts

@InputType()
export class CreatePostInput {
  @Field()
  readonly title!: string;

  @Field()
  readonly image!: string;

  @Field(() => [String])
  readonly tags!: string[];

  @Field(() => [CreateSectionInput])
  readonly sections!: CreateSectionInput[];
}

更新 1

我发现如果我在部分正文中传递一个空数组,那么创建帖子就没有问题。我在下面附上示例查询:

mutation {
  createPost(postInput: {
    image: "newly created.jpg",
    title: "newly created"
    tags: ["newly created, ref"]
    sections: []
  }) {
    _id
    image
    title
    tags
    sections {
      title
      body
      code
      image
    }
  }
}

【问题讨论】:

    标签: typescript mongoose nestjs typegraphql typegoose


    【解决方案1】:

    我发现我不应该使用Ref&lt;&gt; 来声明嵌套文档。为此,您必须使用 items 并提供正确的 typgoose 模型类。在文档中,您可以看到 items 仅适用于原始类型,但在 items 文档描述的末尾,您可以看到您也可以将其用于 typegoose 类(例如SectionModel.ts)。下面我附上了一个解决我问题的例子。

    post.model.ts:

    @ObjectType()
    export class PostModel {
      @Field(() => ID)
      readonly _id: ObjectId;
    
      @Field()
      @prop({required: true})
      title: string;
    
      @Field()
      @prop({nullable: true})
      image: string;
    
      @Field(() => [String])
      @arrayProp({items: String})
      tags: string[];
    
      @Field(() => [SectionModel])
      @arrayProp({items: SectionModel})
      sections: SectionModel[];
    }
    

    section.model.ts:

    @ObjectType()
    export class SectionModel {
      @Field(() => ID)
      readonly _id: ObjectId;
    
      @Field()
      @prop({ required: true })
      title: string;
    
      @Field(() => [String])
      @arrayProp({ items: String })
      body: string[];
    
      @Field(() => [String])
      @arrayProp({ items: String })
      code: string[];
    
      @Field(() => [String])
      @arrayProp({ items: String })
      image: string[];
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2015-01-01
      • 2019-09-24
      • 2018-07-12
      • 2022-01-23
      相关资源
      最近更新 更多