验证对象必须至少有一个operator / meteor mongo

时间:2015-04-10 16:33:10

标签: mongodb meteor

我为一个集合写了一个用户地址的方法。但是,我一直收到错误:

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

这是我的架构:

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

这是我的addAddress事件:

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

这是我的addAddress方法:

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

注意 - 当我执行console.log(addressDetails)时,它会打印出地址详细信息。

有人可以帮忙吗?提前谢谢!!

2 个答案:

答案 0 :(得分:2)

请尝试以下代码:

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

答案 1 :(得分:2)

是的,那个错误有点发送给你错误的方向。无论如何,您在对象上使用$addToSet。这样做:

Meteor.users.update(currentUserId, {$set: addressDetails.address}