Nothing important
Nothing important

Reputation: 301

Nested Objects with Meteor Simpleschema

I'm new to Meteor and am trying to use SimpleSchema /Autoforms in an app I am building. When a user creates a Group, they fill out a group name, description, and location (address). Behind the scenes they are added as the first member of the group, and the address will be converted into lat/lng values soon.

When I try saving this, location and members[0] are empty objects.

Schema:

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

Insert Form:

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

I've seen some similar questions posted to Stack Overflow about this, but the data always seems more obfuscated from the problem at hand. Am I missing something obvious?

Upvotes: 0

Views: 1231

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

You want to nest an actual schema instead of a plain object:

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

Upvotes: 2

Related Questions