【发布时间】:2021-09-19 21:29:18
【问题描述】:
我有一个 Item 模型和一个 Category 模型。
item 模型对 Category 模型有一个reference (ObjectId)。
我正在编写代码来获取特定类别中的项目。
所以我将类别的 id 作为服务的参数(字符串类型),
然后写“return this.ItemModel.find({category: id}).exec();”。
其中“类别”是对类别模型的引用,
'id' 是传递给 API 调用的 id。
我收到错误“No Overload Matches This Call”。
我该如何解决这个问题?
项目架构
export type ItemDocument = Item & Document;
@Schema({ timestamps: true })
export class Item {
@Prop()
name_en: string;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Category' })
category: Category;
}
export const ItemSchema = SchemaFactory.createForClass(Item);
类别架构
export type CategoryDocument = Category & mongoose.Document;
@Schema({ timestamps: true })
export class Category {
@Prop()
name_en: string;
}
export const CategorySchema = SchemaFactory.createForClass(Category);
category.service.ts
@Injectable()
export class CategoryService {
constructor(
@InjectModel(Category.name)
private categoryModel: mongoose.Model<CategoryDocument>,
@InjectModel(Item.name)
private readonly ItemModel: mongoose.Model<ItemDocument>,
) {}
findOneCatItems(id: string) {
return this.ItemModel.find({category: id}).exec(); --> Error Line
}
}
【问题讨论】:
-
你能分享你得到的完整错误吗? Mongoose supports passing filter objects
标签: javascript mongoose nestjs nestjs-mongoose