我有这个模型:
var accountSchema = new mongoose.Schema({
'seeker': {
'fullName': String,
'ageGroup': String,
'education': String,
'lookingForWork': String,
'employmentStatus': String,
'resume': String, // Filename ans stuff
'mainWorkExp': String,
},
'employer': {
'activationStatus': String,
'companyName': String,
'contactPersonFullName': String,
'companyWebsite': String,
'hiringRegion': [String],
'logo': [],
'description': String,
'industry': String,
'workingWithEOSP': Boolean,
'booth': {
'exists': Boolean, // Boolean to store with the employer created his/her booth or not
'numberVisits': Number // number of clicks on the particular booth
/* We can add more attributes here as we go */
}
},
});
它已导出,一切都很好。但是有一个问题。如果我尝试使用此脚本创建寻找者帐户:
new account({
'seeker': {
'fullName': r.fullName,
'ageGroup': r.ageGroup,
'education': r.education,
'lookingForWork': r.lookingForWork,
'employmentStatus': r.employmentStatus,
'mainWorkExp': r.mainWorkExp,
'resume': r.file,
},
}),
r.password,
实际上我是在数据库中得到的:
{
"_id": {
"$oid": "5b4689ab6d68e81d32fda65e"
},
"seeker": {
"fullName": "Alex Ironside",
"ageGroup": "18 - 24",
"education": "Some High School",
"lookingForWork": "Less than 1 month",
"employmentStatus": "Unemployed",
"mainWorkExp": "Business, Finance or Administration. Includes all levels of office admin, accounting/bookkeeping, human resources, banking, etc."
},
"employer": { // The problem is right here!
"hiringRegion": [],
"logo": []
}
}
因此,正如您所看到的,我没有在代码中的任何地方提及logo
或hiringRegion
,但仍然可以创建它。这是为什么?是否与它们都在模型中都声明为array
的事实有关?我该如何解决这个问题?
我基本上不希望在我的代码创建的文档中创建employer
属性/对象。它应该只是seeker
对象,但是我不能仅仅从模型中删除logo
和hiringRegions
,因为我在其他地方使用它们作为数组。
r
对象是req.body
的缩写。
答案 0 :(得分:1)
猫鼬数组implicitly have a default value of empty array,因此您可以立即开始使用该数组。改为这样做:
'hiringRegion': { type: [String], default: undefined },
'logo': { type: [], default: undefined }