mongoose 图形错误:ID不能表示值:"< Buffer...>"

mec1mxoz  于 2023-01-31  发布在  Go
关注(0)|答案(2)|浏览(113)

我有一个基本的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);
  }
}

事件日期:

@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模式出现问题...但我找不到合适的方法来修复它。

ufj5ltwl

ufj5ltwl1#

它实际上不得不与creator字段未填充的事实。
变更自

async findAll(): Promise<Event[]> {
    return this.eventModel
      .find()
      .exec();
  }

async findAll(): Promise<Event[]> {
    return this.eventModel
      .find()
      .populate('creator')
      .exec();
  }

修复了我的问题错误信息有点误导。

nr7wwzry

nr7wwzry2#

确保填充包含要显示的id的字段。

相关问题