【问题标题】:Nested Objects with Meteor Simpleschema使用 Meteor Simpleschema 嵌套对象
【发布时间】:2017-06-29 08:53:09
【问题描述】:

我是 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 上看到过一些类似的问题,但与手头的问题相比,数据似乎总是更加模糊。我错过了什么明显的东西吗?

【问题讨论】:

    标签: meteor meteor-autoform simple-schema


    【解决方案1】:

    你想嵌套一个实际的模式而不是一个普通的对象:

    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
      }
    }));
    

    【讨论】:

    • 是我还是你的会员反对有点乱?不应该是type: memberTypes吗?另外,是否可以使用 SimpleSchema 内联,例如 type: new SimpleSchema({...
    • [memberTypes] 表示由模式memberTypes 定义的对象数组。我确信内联模式会起作用,但我个人可能会发现它更难阅读(特别是如果我有一个数组)。另外,我倾向于在多个集合中重用类型。
    • 啊,好吧,有道理。下面的}], 肯定是错字?
    猜你喜欢
    • 1970-01-01
    • 2016-02-07
    • 2017-02-28
    • 2015-03-13
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多