带有Meteor Simpleschema的嵌套对象
我是Meteor的新手,正在尝试在我正在构建的应用程序中使用SimpleSchema/Autoforms。当用户创建组时,他们会填写组名、描述和位置(地址)。在幕后,它们被添加为组的第一个成员,并且地址很快将转换为lat/lng值 当我尝试保存时,带有Meteor Simpleschema的嵌套对象,meteor,meteor-autoform,simple-schema,Meteor,Meteor Autoform,Simple Schema,我是Meteor的新手,正在尝试在我正在构建的应用程序中使用SimpleSchema/Autoforms。当用户创建组时,他们会填写组名、描述和位置(地址)。在幕后,它们被添加为组的第一个成员,并且地址很快将转换为lat/lng值 当我尝试保存时,location和members[0]是空对象 模式: Groups = new Mongo.Collection('groups'); Groups.attachSchema(new SimpleSchema({ groupName: { t
location
和members[0]
是空对象
模式:
Groups = new Mongo.Collection('groups');
Groups.attachSchema(new SimpleSchema({
groupName: {
type: String,
label: "Group Name",
max: 200
},
createdBy: {
type: String,
autoform: {
omit: true
}
},
members: {
type: [{
_id: {type: String},
firstName: {type: String},
lastName: {type: String}
}],
label: "Group Members",
autoform: {
omit: true
}
},
location: {
type: {
address: {type: String},
lat: {type: String},
lng: {type: String}
},
label: "Location"
},
description: {
type: String,
label: "Group Description",
max: 250,
optional: true
}
}));
插入表格:
Template.groupsList.events({
'submit form': function(e) {
console.log('submitting form..');
e.preventDefault();
var group = {
groupName: $(e.target).find('[name=groupName]').val(),
createdBy: Meteor.userId(),
members: [{
_id: Meteor.userId(),
firstName: Meteor.user().profile.firstName,
lastName: Meteor.user().profile.lastName
}],
location: setLocation($(e.target).find('[name=location]').val()),
description: $(e.target).find('[name=description]').val()
};
function setLocation(location) {
return {
location: location,
lat: 123,
lng: 123
};
}
console.log(group);
var groupId = Groups.insert(group);
Router.go('/group/' + groupId);
}
});
我已经在Stack Overflow上看到了一些类似的问题,但是数据总是因为手头的问题而变得更加模糊。我是否遗漏了一些明显的内容?您希望嵌套一个实际的模式而不是普通对象:
Groups = new Mongo.Collection('groups');
const memberTypes = new SimpleSchema({
_id: {type: String},
firstName: {type: String},
lastName: {type: String}
});
const locationType = new SimpleSchema({
address: {type: String},
lat: {type: String},
lng: {type: String}
});
Groups.attachSchema(new SimpleSchema({
groupName: {
type: String,
label: "Group Name",
max: 200
},
createdBy: {
type: String,
autoform: {
omit: true
}
},
members: {
type: [memberTypes],
label: "Group Members",
autoform: {
omit: true
}
},
location: {
type: locationType,
label: "Location"
},
description: {
type: String,
label: "Group Description",
max: 250,
optional: true
}
}));
要嵌套实际架构而不是普通对象:
Groups = new Mongo.Collection('groups');
const memberTypes = new SimpleSchema({
_id: {type: String},
firstName: {type: String},
lastName: {type: String}
});
const locationType = new SimpleSchema({
address: {type: String},
lat: {type: String},
lng: {type: String}
});
Groups.attachSchema(new SimpleSchema({
groupName: {
type: String,
label: "Group Name",
max: 200
},
createdBy: {
type: String,
autoform: {
omit: true
}
},
members: {
type: [memberTypes],
label: "Group Members",
autoform: {
omit: true
}
},
location: {
type: locationType,
label: "Location"
},
description: {
type: String,
label: "Group Description",
max: 250,
optional: true
}
}));
是我还是你的成员反对有点混乱?是否应该是
类型:memberTypes
?另外,是否可以内联使用SimpleSchema,如type:new SimpleSchema({…
?[memberTypes]
表示模式memberTypes
定义的对象数组。我确信内联模式会起作用,但我个人可能会发现它更难阅读(特别是如果我有一个数组的话)。另外,我倾向于在多个集合中重复使用类型。啊,好吧,这是有道理的。虽然下面的}],
肯定是一个输入错误?是我还是你的成员对象有点混乱?是否应该是type:memberTypes
?还有,是否可以使用SimpleSchema内联,比如type:new SimpleSchema({…
?[memberTypes]
表示由模式定义的对象数组memberTypes
。我确信内联模式会起作用,但我个人可能会发现它更难阅读(特别是如果我有一个数组的话)。此外,我倾向于在多个集合中重复使用类型。啊,好的,这是有意义的。],
下面肯定是打字错误?