Typegoose类定义的静态方法返回空数组



当我使用findByUserId定义的静态方法时,我会得到空的数组值,而如果我直接使用模型,它将成功检索对象。

export default class Cart {

@prop({required: true, unique: true})
public userId!: String

@prop()
public items?: [Item]

public static async findByUserId(this: ReturnModelType<typeof Cart>, userId: string) {
try {
return await this.find({itemId: userId});
} catch (err) {
return null;
}
}
}
export const CartModel = getModelForClass(Cart);

// RETURNS EMPTY ARRAY
const user = await CartModel.findByUserId("asdasdsa21321")
// WORKS
const user1 = await CartModel.find({userId:"asdasdsa21321"})

有什么想法吗?

问题是我使用了无效的密钥。

return await this.find({itemId: userId});

所以我把它换成

return await this.find({userId: userId});

最新更新