私のプロジェクトでは、mongodb で mikro orm を使用しています。これまでのところ、typescript での MongoDB に最適な orm です。しかし、オブジェクト 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
は正常に機能するため、タイプの問題です。