简体   繁体   中英

How to set false as a value using Boolean in SimpleSchema?

SimpleSchema solves a great general problem but man am I confused by it sometimes.

For instance, I have this value defined there:

changedPassword: {
    type: Boolean,
    optional: true
}

It tracks if the user has changed his password, and should obviously be either true or false . However, SimpleSchema doesn't seem to share my view on what Boolean means.

Anyway, changedPassword starts out not existing at all when someone creates a user, and is set to true when he changes his password. The problem arrives when I'm doing a password reset, and the value should revert to false or non-existance again. SimpleSchema doesn't seem to allow any changes to this field except setting it to true !

My method may look like this,

changedPassword: function(status) {
  if (status === false) {
    Meteor.users.update({_id: Meteor.userId()}, {$unset: {changedPassword: ''}})
  }
}

or

changedPassword: function(status) {
  if (status === false) {
    Meteor.users.update({_id: Meteor.userId()}, {$set: {changedPassword: false}}
  }
}

but it still won't actually make the change, even when I'm logged in and typing Meteor.call('changedPassword', false) in the console.

What the heck is the purpose of that and how can I get around it?

I got around it by simply making another method:

notChangedPassword: function() {
  Meteor.users.update({_id: Meteor.userId()}, {$set: {changedPassword: false}})
}

Don't know it wouldn't work with a conditional, though. Looks like a bug. I'll leave this answer here until someone can give a better, more thorough one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM