Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation object must have at least one operator / meteor mongo

Tags:

mongodb

meteor

I wrote a method to a user's address to a collection. However, i keep getting the error:

When the modifier option is true, validation object must have at least one operator.

Here is my schema:

var Schemas = {};

Schemas.UserAddress = new SimpleSchema({

streetAddress: {
    type: String,
    max: 100,
    optional: false
},
city: {
    type: String,
    max: 50,
    optional: false
},
state: {    
    type: String,
    regEx: /^[a-zA-Z-]{2,25}$/,
    optional: false
},
zipCode: {
type: String,
regEx: /^[0-9]{5}$/,
optional: false
}
  });

Schemas.User = new SimpleSchema({
emails: {
    type: [Object],
    optional: false
},
"emails.$.address": {
    type: String,
    regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
    type: Boolean
},
createdAt: {
    type: Date
},
profile: {
    type: Schemas.UserProfile,
    optional: false
},
   address: {
    type: Schemas.UserAddress,
    optional: false
},
services: {
    type: Object,
    optional: true,
    blackbox: true
}
});

Meteor.users.attachSchema(Schemas.User);

And here is my addAddress event:

Template.editAddress.events({

  'click .addAddress': function(e, tmpl) {
e.preventDefault();
var currentUserId = this._id; 
var addressDetails = {
  address: {
    streetAddress: $('#streetAddress').val(),
    city: $('#city').val(),
    state: $('#state').val(),
    zipCode: $('#zipCode').val()
  },
};
console.log(addressDetails);
Meteor.call('addNewAddress', addressDetails, currentUserId, function(error) {
  if (error) {
    alert(error.reason);
  } else {
    console.log('success!');
    Router.go('Admin');
  }
});
},
});

Here's my addAddress method:

Meteor.methods({
'addNewAddress': function(addressDetails, currUserId) {

    var currentUserId = currUserId;

    Meteor.users.update(currentUserId, {$addToSet:
        {'address.streetAddress': addressDetails.streetAddress,
         'address.city': addressDetails.city,
         'address.state': addressDetails.state,
         'address.zipCode': addressDetails.zipCode
        }
    });
}
});

Note - when I do console.log(addressDetails), it prints out the address details just fine.

Can someone help? Thank you in advance!!

like image 509
Trung Tran Avatar asked Sep 28 '22 08:09

Trung Tran


2 Answers

Yeah, that error kinda sends you in the wrong direction. Regardless, you're using $addToSet on an object. Do this:

Meteor.users.update(currentUserId, {$set: addressDetails.address}
like image 63
Matt K Avatar answered Dec 09 '22 11:12

Matt K


Try the following code:

Meteor.users.update(
  {$currUserId}, 
  {$addToSet:
    {'address.streetAddress': addressDetails.streetAddress,
     'address.city': addressDetails.city,
     'address.state': addressDetails.state,
     'address.zipCode': addressDetails.zipCode
    }
});
like image 43
Nathan Avatar answered Dec 09 '22 11:12

Nathan