0

我在我的项目中使用 mikro orm 和 mongodb。到目前为止,它是 MongoDB 在 typescript 中的最佳形式。但是我在使用带有对象 ID 的 $in 时遇到类型错误。这是我的代码:

  @Query(() => [Post])
  async allPosts(@Ctx() { em }: appContext) {
    const posts = await em.find(Post, {});
    const repo = em.getRepository(Post)
    const multiplePosts = await repo.find({ _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] }  });
    console.log("multiplePosts: ", multiplePosts);
    return posts;
}

Post.ts

export class Post {

  @Field(() => ID)
  @PrimaryKey()
  _id!: string;

  @Field(() => String)
  @Property()
  createdAt = new Date();

  @Field(() => String)
  @Property({ onUpdate: () => new Date() })
  updatedAt = new Date();

  @Field(() => String)
  @Property()
  title!: string;

  @Field(() => String)
  @Property()
  excerpt!: string;

  @Field(() => String)
  @Property()
  content!: string;

  @Field()
  @ManyToOne()
  author!: User;
}

这是我得到的错误:

src/resolvers/PostResolver.ts:32:43 - error TS2769: No overload matches this call.
  Overload 1 of 2, '(where: FilterQuery<Post>, options?: FindOptions<Post, any> | undefined): Promise<Post[]>', gave the following error.      
    Argument of type '{ _id: { $in: ObjectId[]; }; }' is not assignable to parameter of type 'FilterQuery<Post>'.
      Types of property '_id' are incompatible.
        Type '{ $in: ObjectId[]; }' is not assignable to type 'string | RegExp | OperatorMap<FilterValue2<string>> | FilterValue2<string>[] |n 
ull | undefined'.
          Types of property '$in' are incompatible.
            Type 'ObjectId[]' is not assignable to type 'FilterValue2<string>[]'.
              Type 'ObjectId' is not assignable to type 'FilterValue2<string>'.
                Type 'ObjectId' is missing the following properties from type 'RegExp': exec, test, source, global, and 13 more.
  Overload 2 of 2, '(where: FilterQuery<Post>, populate?: any, orderBy?: QueryOrderMap | undefined, limit?: number | undefined, offset?: number | undefined): Promise<...>', gave the following error.
    Argument of type '{ _id: { $in: ObjectId[]; }; }' is not assignable to parameter of type 'FilterQuery<Post>'.
      Types of property '_id' are incompatible.
        Type '{ $in: ObjectId[]; }' is not assignable to type 'string | RegExp | OperatorMap<FilterValue2<string>> | FilterValue2<string>[] | null | undefined'.
          Types of property '$in' are incompatible.
            Type 'ObjectId[]' is not assignable to type 'FilterValue2<string>[]'.

32     const multiplePosts = await repo.find({
                                             ~
33       _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] },
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34     });
   ~~~~~


    at createTSError (D:\blog\server\node_modules\ts-node\src\index.ts:513:12)
    at reportTSError (D:\blog\server\node_modules\ts-node\src\index.ts:517:19)
    at getOutput (D:\blog\server\node_modules\ts-node\src\index.ts:752:36)
    at Object.compile (D:\blog\server\node_modules\ts-node\src\index.ts:968:32)
    at Module.m._compile (D:\blog\server\node_modules\ts-node\src\index.ts:1056:42)
    at Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Object.require.extensions.<computed> [as .ts] (D:\blog\server\node_modules\ts-node\src\index.ts:1059:12)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:997:19)

删除 ObjectId 会使 typescript 编译器满意,但 MongoDB 不会将它们视为对象 ID。

编辑:另外,添加//ts-ignore工作正常,所以这是一个类型问题。

4

1 回答 1

1

问题来自您的实体定义,您在其中明确地说_id类型是string,而不是ObjectId。因此,如果它是一个对象 id,请修复定义:

  @PrimaryKey()
  _id!: ObjectId;

那么您的查询应该会通过。请注意,您还可以定义序列化的 PK id: string,它将充当 ObjectId 的字符串版本的虚拟 getter。

https://mikro-orm.io/docs/usage-with-mongo/

您可能需要安装@types/mongodb.

一般来说,如果属性类型是ObjectId,也可以通过字符串形式进行查询。这些应该是相等的:

repo.find({ _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] } });
repo.find({ _id: { $in: ["6005d0c253e2b72af07fc61a"] } });
repo.find({ _id: [new ObjectId("6005d0c253e2b72af07fc61a")] });
repo.find({ _id: ["6005d0c253e2b72af07fc61a"] });
repo.find([new ObjectId("6005d0c253e2b72af07fc61a")]);
repo.find(["6005d0c253e2b72af07fc61a"]);

另请注意,除非您使用 ts-morph,否则您应该在使用属性初始化程序的地方放置显式类型:

  @Property()
  createdAt: Date = new Date();

https://mikro-orm.io/docs/metadata-providers/#limitations-and-requirements

于 2021-01-19T08:40:37.280 回答