验证失败:转换为数组的值失败

Posted

技术标签:

【中文标题】验证失败:转换为数组的值失败【英文标题】: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
    
  

【问题讨论】:

【参考方案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[];

【讨论】:

以上是关于验证失败:转换为数组的值失败的主要内容,如果未能解决你的问题,请参考以下文章

验证转换为数字失败

mongoose .create 给 node.js “ValidationError:验证失败:转换为 [undefined] 值失败......”

具有渲染属性的jsf组件验证失败

Struts(二十四):短路验证&重写实现转换验证失败时短路&非字段验证

你如何确定哪个验证器失败了?

Mongoose 5.0.15 / MongoDB 3.6.4 转换为数组失败的值 [NaN,NaN]