【问题标题】:Can not Query all users because of MongoDB id由于 MongoDB id 无法查询所有用户
【发布时间】:2021-10-12 18:53:34
【问题描述】:

我正在编写一个内置于 TypeScript 和 TypeGoose 的 CRUD API。
我收到一条错误消息,

CannotDetermineGraphQLTypeError:无法确定“用户”类的“_id”的 GraphQL 输出类型。用作其 TS 类型或显式类型的值是用适当的装饰器装饰的还是适当的输出值?

我有一个用户实体。

import { Field, ObjectType } from 'type-graphql';
import { ObjectId } from 'mongodb';
import { prop as Property, getModelForClass } from '@typegoose/typegoose';

@ObjectType()
export class User {
  @Field()
  readonly _id: ObjectId;

  @Field()
  @Property({ required: true })
  email: string;

  @Field({ nullable: true })
  @Property()
  nickname?: string;

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

  constructor(email: string, password: string) {
    this.email = email;
    this.password = password;
  }
}

export const UserModel = getModelForClass(User);

这就是我的查询解析器的样子。

@Query(() => [User])
  async users() {
    const users = await UserModel.find();
    console.log(users);
    return users;
  }

我该如何解决这个问题?好像 TypeGraphQL 不明白 MongoDB ID 是什么?

【问题讨论】:

    标签: mongodb graphql typegraphql typegoose


    【解决方案1】:

    我不确定这一点,但也许 ObjectId.toString() 可以帮助你。 MongoDB doc about ObjectId.toString()

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2020-03-28
    • 2021-09-07
    • 2011-11-18
    • 2019-08-18
    • 2020-05-22
    相关资源
    最近更新 更多