gpt4 book ai didi

mongoose - GraphQLError : ID cannot represent value: "

转载 作者:行者123 更新时间:2023-12-04 13:12:51 27 4
gpt4 key购买 nike

我有一个基本的 Nestjs - Mongoose - Graphql api,我定义了两个模式:UserEvent

//USER Schema
@Schema()
export class User extends Document {
@Prop()
username: string;

@Prop({ required: true })
password: string;

@Prop({ required: true, unique: true })
email: string;

@Prop({ required: true, unique: true })
handle: string;

@Prop()
avatar: string;
}

export const UserSchema = SchemaFactory.createForClass(User);
//EVENT schema
@Schema()
export class Event extends Document {
@Prop({
type: MongooseSchema.Types.ObjectId,
ref: User.name,
required: true,
})
creator: GqlUser;

@Length(5, 30)
@Prop({ required: true })
title: string;

@Length(5, 200)
@Prop({ required: true })
description: string;

@Prop()
createdDate: string;

@Prop()
public: boolean;

@Prop()
active: boolean;
}

export const EventSchema = SchemaFactory.createForClass(Event);

EventSchema , 字段 creator键入为 MongooseSchema.Types.ObjectId指着 User我的 events.resolvers.ts看起来像这样:

@Resolver(of => GqlEvent)
export class EventsResolvers {
constructor(private eventsService: EventsService) {}

@Query(returns => [GqlEvent])
async events() {
return this.eventsService.findAll();
}

@Mutation(returns => GqlEvent)
async createEvent(
@Args('createEventInput') createEventInput: CreateEventDto,
) {
return this.eventsService.create(createEventInput);
}
}

事件 Dtos :
@ObjectType()
export class GqlEvent {
@Field(type => ID)
id: string;

@Field(type => GqlUser)
creator: GqlUser;

@Field()
title: string;

@Field()
description: string;

@Field()
createdDate: string;

@Field()
public: boolean;

@Field()
active: boolean;
}

@InputType()
export class CreateEventDto {
@Field(type => ID)
creator: GqlUser;

@Field()
@Length(5, 30)
title: string;

@Field()
@Length(5, 200)
description: string;

@Field()
@IsBoolean()
public: boolean;
}
这样,Nestjs 生成以下 gql 模式(为了清楚起见,我跳过了与用户 CRUD 相关的部分):
# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------

type GqlUser {
id: ID!
username: String!
handle: String!
avatar: String!
email: String!
}

type GqlEvent {
id: ID!
creator: GqlUser!
title: String!
description: String!
createdDate: String!
public: Boolean!
active: Boolean!
}

type Query {
events: [GqlEvent!]!
}

type Mutation {
createEvent(createEventInput: CreateEventDto!): GqlEvent!
}


input CreateEventDto {
creator: ID!
title: String!
description: String!
public: Boolean!
}

什么有效: createEvent突变在数据库中正确插入文档:
{
"_id":{"$oid":"5f27eacb0393199e3bab31f4"},
"creator":{"$oid":"5f272812107ea863e3d0537b"},
"title":"test event",
"description":"a test description",
"public":true,
"active":true,
"createdDate":"Mon Aug 03 2020",
"__v":{"$numberInt":"0"}
}

我的问题:当我尝试请求 creator 的子字段时出现以下错误:
Gql 查询:
query {
events {
id
creator {
id
}
createdDate
public
description
title
active
}
}

回复 :
"errors": [
{
"message": "ID cannot represent value: <Buffer 5f 27 28 12 10 7e a8 63 e3 d0 53 7b>",
"locations": [
{
"line": 6,
"column": 7
}
],
"path": [
"createEvent",
"creator",
"id"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"message": "ID cannot represent value: <Buffer 5f 27 28 12 10 7e a8 63 e3 d0 53 7b>",
"stacktrace": [
"GraphQLError: ID cannot represent value: <Buffer 5f 27 28 12 10 7e a8 63 e3 d0 53 7b>",...

因为当我省略时它工作正常 creator领域,我懂 Mongoose MongooseSchema.Types.ObjectId导致 gql 模式出现问题...但我找不到合适的方法来修复它。提前寻求帮助

最佳答案

它实际上与 creator 的事实有关。字段未填充。
改变自

async findAll(): Promise<Event[]> {
return this.eventModel
.find()
.exec();
}
async findAll(): Promise<Event[]> {
return this.eventModel
.find()
.populate('creator')
.exec();
}
解决了我的问题。错误信息有点误导。

关于mongoose - GraphQLError : ID cannot represent value: <Buffer. ..>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63228105/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com