Meteor Collection2 Update throwing errors

697 Views Asked by At

I'm using the Collection2 package for integrating a schema and simple validation method for my Meteor app.

Inserts are working just fine, erroring appropriately, and inserting when valid as intended. According to the docs, using the update method is pretty much the same but validation errors keep appearing even though everything is valid per the schema.

So for example, I'll submit a new property(an actual home/apartment) that validates against the schema, and it works no problem. I'll go to edit that property, change one letter on 1 field, and it errors on various fields.

I'm kind of at a loss. It's pretty straightforward. I was updating documents no problem before introducing Collection2, but I don't think it's a problem with the package per se because I know it's in use and actively updated. Maybe I need to put this method on the server?

Any help would be appreciated. Thanks!

The Client side JS file:

Template.propertyEdit.events({
  'submit form': function(e){
    e.preventDefault();

    var property_details = {
      name: $(e.target).find('[name=name]').val(),
      address: $(e.target).find('[name=address]').val(),
      city: $(e.target).find('[name=city]').val(),
      state: $(e.target).find('[name=state]').val(),
      zipcode: $(e.target).find('[name=zipcode]')
    }

    Properties.update(this._id, {$set: property_details}, function(error,result){
      if(error){
        for(var i=0; Properties.simpleSchema().namedContext().invalidKeys().length > i; i++ ){
          throwError(Properties.simpleSchema().namedContext().invalidKeys()[i].message);
        }
      }else{
        alert("Property details updated");
        Router.go('propertyOverview', {_id: result});
      }
    });

  });

The Collection:

Properties = new Meteor.Collection2('properties', {
  schema: {
    name: {
        type: String,
        label: "Property Name",
        min: 1
    },
    address: {
        type: String,
        label: "Address",
        min: 1
    },
    city: {
        type: String,
        label: "City",
        min: 1
    },
    state: {
        type: String,
        label: "State",
        min: 2,
        max: 2
    },
    zipcode: {
        type: String,
        label: "Zip Code",
        min: 5,
        max: 11
    },
    userId: {
        type: String,
        label: "User Id",
        min: 8
    }
  }
});
1

There are 1 best solutions below

1
On

Finally saw the issue.. in the template, the HTML, the placeholder attribute seems to register as a value even though the value attribute has content..

I had:

<input name="zipcode" type="text" class="form-control" value="{{zipcode}}" placeholder="Enter a Zip Code" />

Changed it to:

<input name="zipcode" type="text" class="form-control" value="{{zipcode}}" />

And it worked : )

Not sure if this something that is just known, and I should have known, or a bug. I'll submit it regardless.