使用 Meteor Simpleschema 的嵌套对象

Nested Objects with Meteor Simpleschema

我是 Meteor 的新手,正在尝试在我正在构建的应用程序中使用 SimpleSchema /Autoforms。当用户创建群组时,他们会填写群组名称、描述和位置(地址)。在幕后,他们被添加为组的第一个成员,地址将很快转换为 lat/lng 个值。

当我尝试保存它时,locationmembers[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
  }
}));