如何在SimpleSchema Meteor中定义具有自动值的子文档,而不将其插入到每个父文档插入中
How to define a subdocument in SimpleSchema Meteor with an autovalue without inserting it on each parent document insert?
我正试图为具有子文档的集合定义一个模式,父文档和子文档都有自动值字段,这些字段应该在插入时设置。问题是,当我尝试插入一个新的父文档(没有任何子文档)时,我会收到一个错误,指出子文档字段是必需的。
以下是再现问题的完整代码:
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>
我试着在"聊天消息"中添加一个"可选:true",但没有解决问题。似乎即使不包括子文档,子文档autovalue仍然会被执行,并用生成的值创建一个新的子文档。
如何使用具有自动值的子文档正确创建文档?
可能需要使schema_ChatRooms_ChatMesssage中的所有字段都是可选的,并通过自动表单提交。
相关文章:
- 收集订阅者的流星事件,用于在 mongodb 中新插入文档
- 谷歌应用程序脚本:如何修复循环将一个谷歌文档的段落插入另一个
- 在post请求中使用mongodb插入多个文档
- 如何使用vanillaJS将mp4视频插入到具有特定维度的html文档中,播放2秒,然后删除节点
- 追加到文档中尚未插入的新节点
- 在流星集合文档中插入值
- 通过node-js脚本将文档数组插入mongodb中的数据库时出现问题
- MongoDB在将文档插入数据库之前为空集合
- 使用 mongoDB 插入子文档
- 如果子文档值不存在,则 Mongo DB 插入到子文档中
- 创建猫鼬模式并插入文档
- 我正在尝试遍历 id 数组,如果不存在 id,则插入一个 mongodb 文档.我怎样才能让他们坚持下去
- 通过angularjs在mongodb中插入嵌入式文档
- MongoDB使用Map Reduce插入来自其他集合的嵌入式文档
- 将一个对象作为相同的文档插入到MONGO中
- 打印 HTML 文档时插入页眉/页脚
- 在MongoDB中插入/向上插入嵌套文档
- 如果找不到文档,则不插入,而是更新插入
- 如何在SimpleSchema Meteor中定义具有自动值的子文档,而不将其插入到每个父文档插入中
- 在使用猫鼬的文档插入中出现问题