我在我的项目中使用 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
工作正常,所以这是一个类型问题。