【发布时间】:2020-08-30 20:20:52
【问题描述】:
我正在做一个使用以下项目的项目:
"@nestjs/core": "^7.0.0",
"@nestjs/mongoose": "^7.0.0",
"mongoose": "^5.9.12",
// ...
"typescript": "^3.7.4",
使用 mongoose/mongoDB 配置:
uri: MONGO_DB_URI,
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
我正在尝试为此模型构建一个简单的 CRUD:
export const ContactSchema = new mongoose.Schema(
{
source_id: { type: String, required: true },
firstName: { type: String, trim: true },
lastName: { type: String, trim: true },
phones: [
{
number: {
type: String,
required: true,
unique: true,
validate: {
validator: function(value) {
const phoneNumber = parsePhoneNumberFromString(value)
return phoneNumber && phoneNumber.isValid()
},
},
},
type: {
type: String,
default: function() {
return parsePhoneNumberFromString(this.number).getType() || "N/A"
},
},
code: {
type: Number,
default: function() {
return parsePhoneNumberFromString(this.number).countryCallingCode || undefined
},
},
national: {
type: Number,
default: function() {
return parsePhoneNumberFromString(this.number).nationalNumber || undefined
},
},
},
],
email: { type: String, unique: true, required: true, lowercase: true, trim: true },
},
{ timestamps: true },
)
ContactSchema.plugin(mongoosePaginate)
像每个 CRUD 应用程序一样,我愿意让 fildAll() 和 fildOne() 路由返回给定 Contact 的正文 他的所有信息,包括其电话号码列表 .所以我用了:
// ...
async findAll(): Promise<Contact[]> {
return this.contactModel.find()
// then I add
.populate('phones')
}
async findBySourceId(id: string): Promise<Contact> {
return this.contactModel.findOne({ source_id: id })
// then I add
.populate('phones')
}
// ...
所有信息都很好地保存在数据库中,并且没有丢失数据(两部手机),而且我确信它甚至可以在不添加 .poplate('x') 的情况下工作,但它在某处发生了变化,它现在返回未填充电话阵列。
现在它返回了:
{
"_id": "5ebc22072e18637d84bcf6f0",
"firstName": "Maher",
"lastName": "Boubakri",
"phones": [],
"email": "mhb@test.im",
// ...
}
但是,它应该返回:
{
"_id": "5ebc22072e18637d84bcf6f0",
"firstName": "Maher",
"lastName": "Boubakri",
"phones": [
{
"_id": "5ebc22072e18637d8fd948f9",
"number": "+21622123456",
"code": 216,
"type": "MOBILE",
"national": 22123456,
}
],
"email": "mhb@test.im",
// ...
}
注意:很明显,MongoDB 会为每个 phone 对象生成_id,但它不是 ref 对象。
任何想法都会很有帮助,
谢谢。
【问题讨论】:
标签: mongodb typescript mongoose nestjs mongoose-populate