【发布时间】:2016-11-28 15:15:07
【问题描述】:
在我的文档预保存挂钩中,我检查嵌套属性accident 中的所有属性是否存在并且具有正确的值。如果accident 根本不存在也可以。
当我尝试保存在我的文档的嵌套属性 accident 上没有值的文档时,它仍然会进入我的自定义验证规则。这是因为我无法检测到这个嵌套对象是否为空。
架构中的属性
prescriptionSchema = mongoose.Schema({
accident: {
kind: String, #accident, workAccident,
date: Date,
company: String
},
...
预保存挂钩
console.log _.isEqual(data.accident, {}) #false
console.log JSON.stringify data.accident #{}
console.log JSON.stringify data.accident == JSON.stringify {} #false
console.log JSON.stringify {} #{}
console.log Object.keys(data.accident).length #14
for key, value of data.accident
console.log key, value #gives me basically the whole document with functions etc.
当前检测(不好的代码)
if data.accident? && (data.accident['kind'] || data.accident['date'] || data.accident['company']) #=> do validation
种子
newRecipe = new RecipeModel()
for key, value of recipe
newRecipe[key] = value
newRecipe.save((err, result) ->
return next err if err?
return next "No Id" if !result._id?
return next null, result._id
)
我尝试了{}、null 并没有作为recipe.accident 的值。
猫鼬版:4.0.2
节点版本:5.9
【问题讨论】: