我正在尝试将Typegoose与GrqphQL,MongoDB和Nest.js结合使用。我想创建一个可以创建帖子的突变。我为简单的 Post 创建了模型,服务和解析器。当我尝试运行突变以创建帖子时,出现此错误:
PostModel验证失败:部分:无法将值强制转换为数组:
[ [Object: null prototype] { title: 'section 1', body: [ 'section test lalal' ], code: [ "console.log('hello world!)" ], image: [ 'kanow.svg' ] } ]
路径“ sections”中
我不知道为什么会收到我尝试使用的错误: ref , itemsRefs (有关类和字符串值。您可以在此处了解更多信息: typegoose arrayProp)。在创建新的PostModel
和console.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看起来不错,因此它不应成为此错误的来源。
我想知道我在做什么错以及为什么它不起作用。下面我附上了一些代码。如果您还有其他需要,请在评论中告诉我。
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
我发现,如果我在section主体内部传递一个空数组,那么创建帖子是没有问题的。我在下面附加示例查询:
mutation {
createPost(postInput: {
image: "newly created.jpg",
title: "newly created"
tags: ["newly created, ref"]
sections: []
}) {
_id
image
title
tags
sections {
title
body
code
image
}
}
}
答案 0 :(得分:0)
我发现我不应该使用Ref<>
来声明嵌套文档。为此,您必须使用项并提供正确的错别字模型类。在文档中,您可以看到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[];
}