【发布时间】:2020-09-09 15:35:58
【问题描述】:
我有一个名为 subcategory 的架构:
const subCategorySchema = new mongoose.Schema({
name: {
type: String,
required: true
},
description: {
type: String,
required: false
},
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Category',
required: true
},
image: {
type: String,
required: false
}
})
但是在执行此操作时:const subCategoryRecord = await this.subCategoryModel.findById("idAsString") 我得到 null
我读到了其他问题,例如this
(这里说我应该发送new ObjectId('id')而不是string)
但我有另一个名为category 的架构,在通过发送字符串执行findById 时,我确实得到了结果,有什么区别?
这就是类别架构:
const categorySchema = new mongoose.Schema({
name: {
type: String,
required: true
},
description: {
type: String,
required: false
},
image: {
type: String,
required: false
}
})
这个作品:const category = await this.categoryModel.findById('categoryIdAsString')
【问题讨论】:
标签: javascript node.js database mongodb mongoose