流星 - 简单模式丢失对象属性

Meteor - Simple Schema losing Object properties

我有一个带有 aldeed:simple-schemaMongoCollection,其中 content 属性 的类型为 Object:

以下代码将文档写入控制台,然后将其插入,然后获取具有正确 ID 的文档并将其写入控制台:

console.log(doc);
const id = notes.collection.insert(doc);
let newdoc = notes.collection.findOne({_id: id});
console.log(newdoc);

在循环操作期间,内容 属性 的对象内的值丢失。

插入前:

I20160304-16:52:24.722(-5)? { doctorId: 'xD7FiSfYdqwk94gQ6',                                                                                                                                                                              
I20160304-16:52:24.723(-5)?   patientId: '4wG3nnkzrfH4W2hsG',                                                                                                                                                                             
I20160304-16:52:24.723(-5)?   created: 1457128344,                                                                                                                                                                                        
I20160304-16:52:24.723(-5)?   type: 'note',                                                                                                                                                                                               
I20160304-16:52:24.727(-5)?   content: { noteText: 'Test' } }                                                                                                                                                                             

从数据库检索时:

I20160304-16:52:24.734(-5)? { _id: 'w6rRoMqtJc5EKFKFs',                                                                                                                                                                                   
I20160304-16:52:24.735(-5)?   doctorId: 'xD7FiSfYdqwk94gQ6',                                                                                                                                                                              
I20160304-16:52:24.735(-5)?   patientId: '4wG3nnkzrfH4W2hsG',                                                                                                                                                                             
I20160304-16:52:24.735(-5)?   created: 1457128344,                                                                                                                                                                                        
I20160304-16:52:24.736(-5)?   type: 'note',                                                                                                                                                                                               
I20160304-16:52:24.736(-5)?   content: {} } 

我不明白为什么会这样。这是简单模式中内容属性的规范:

Carin.subschemas.object = {
  type: Object,
  optional: false
};

来自SimpleSchema docs

If you have a key with type Object, the properties of the object will be validated as well, so you must define all allowed properties in the schema. If this is not possible or you don't care to validate the object's properties, use the blackbox: true option to skip validation for everything within the object.

基于此,您需要将 blackbox: true 添加到您的架构中:

Carin.subschemas.object = {
  type: Object,
  blackbox: true,
  optional: false
};

或者您需要添加所有适当的字段。

错误原因:你必须提到是否需要验证对象属性

案例 1: 如果您希望验证您的某些属性

add 'optional :true' to fields that need not be validated

//assume you want only x to be validated
Carin.subschemas.object.x:{
  type:String,
  label:"x"
},
Carin.subschemas.object.y:{
  type:String,
  label:"y",
  optional:true 
} 

案例 2 : none 个要验证的属性

Carin.subschemas.object = {
  type: Object,
  blackbox: true,
  optional: false
}