I'm trying to define a schema for a collection with a sub document, both parent and sub documents have autovalue fields that should get set on inserts. The problem is that when I try to insert a new parent document (without any sub documents) I get an error stating that the sub-document fields are required.
Here is the full code to reproduce the problem:
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>
I've tried adding an "optional: true" to "chatMessages" but it didn't solve it. It seems like even when a sub-document is not included the sub-document autovalue still gets executed and creates a new subdocument with the generated values.
What can I do to properly create a document with sub-documents that have auto values?
可能您需要将schema_ChatRooms_ChatMesssage中的所有字段设置为可选,并通过自动形成功能将其忽略。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.