Javascript 如何在SimpleSchema Meteor中定义具有自动值的子文档,而无需在每次父文档插入时插入它?

Javascript 如何在SimpleSchema Meteor中定义具有自动值的子文档,而无需在每次父文档插入时插入它?,javascript,node.js,meteor,meteor-autoform,Javascript,Node.js,Meteor,Meteor Autoform,我正在尝试为具有子文档的集合定义架构,父文档和子文档都具有应在插入时设置的自动值字段。 问题是,当我尝试插入一个新的父文档(没有任何子文档)时,我得到一个错误,指出子文档字段是必需的 以下是重现问题的完整代码: main.js ChatRooms = new Meteor.Collection("chatRooms"); schema_ChatRooms_ChatMesssage = new SimpleSchema({ userId: { type: String,

我正在尝试为具有子文档的集合定义架构,父文档和子文档都具有应在插入时设置的自动值字段。 问题是,当我尝试插入一个新的父文档(没有任何子文档)时,我得到一个错误,指出子文档字段是必需的

以下是重现问题的完整代码:

main.js

ChatRooms = new Meteor.Collection("chatRooms");

schema_ChatRooms_ChatMesssage = new SimpleSchema({
    userId: {
        type: String,
        label: "User ID",
        autoValue: function() {
            if (this.isInsert) {
              if (! this.isFromTrustedCode) {
                return this.userId;
              }
            } else {
              this.unset();
            }},
        autoform: { omit: true }
    },
    content: {
        type: String,
        label: "Content",
        max: 1000,
        min: 1
    },
    creationDate: {
        type: Date,
        label: "Created On",
        autoValue: function() {
            if (!this.isSet) {
                return new Date();
            }
            else {
              this.unset();
            }},
        autoform: { omit: true }
    }
});

schema_ChatRoom = new SimpleSchema({
    name: {
        type: String,
        label: "Name",
        max: 50,
        min: 1
    },
    isPublic: {
        type: Boolean,
        label: "Public"
    },
    creationDate: {
        type: Date,
        label: "Created On",
        autoValue: function() {
            if (!this.isSet) {
                return new Date();
            }
            else {
              this.unset();
            }},
        autoform: { omit: true }
    },
    // Sub Documents
    chatMessages: {
        type: schema_ChatRooms_ChatMesssage,
        label: "Chat Messages",
        optional: true,
        autoform: { omit: true }
    }
});

ChatRooms.attachSchema(schema_ChatRoom);

if (Meteor.isClient) {
    AutoForm.addHooks(null, {
        onError: function(operation, error, template) {
                    alert(operation.toString() + " : " + error.toString());
                }
    });
} 
main.html

<head>
  <title>TestSubDoc</title>
</head>

<body>
  <h1>Create</h1>

  {{> quickForm collection="ChatRooms" id="chatRooms_create_form" type="insert"}}
</body>

测试子文档
创造
{{>QuickFormCollection=“ChatRooms”id=“ChatRooms\u create\u form”type=“insert”}
我尝试在“聊天信息”中添加一个“可选:true”,但没有解决这个问题。 看起来,即使子文档未包含,子文档autovalue仍会执行,并使用生成的值创建新的子文档


如何使用具有自动值的子文档正确创建文档?

可能需要将schema\u ChatRooms\u ChatMessage中的所有字段设置为可选字段,并由autoform指定