title: Updating the email address in Meteor.js user accounts
body: <p>I've been troubleshooting a lot of user errors today, using Meteor.js and the Accounts-ui-bootstrap3 package.  So far my favorite error is the 'Token Expired' on a password reset, but the one that really causes issues is 'User Not Found' when the correct email address is entered in.  It took a while to work out, but the problem is caused by accounts created with the email address in all caps.  Apparently Accounts is case sensitive for email addresses. </p>
  
  <p>Now while I have since updated all of my code to lowercase incoming user emails, I still have to fix the existing users.  I created a server method to deal with this:</p>
  
  <pre><code>Meteor.methods({
    sanitizeEmails: function() {
      Meteor.users.find({
        'emails.address': /[A-Z]{1,}/
      }).map(function(user) {
        console.log(user.emails[0].address);
        Meteor.users.update({
          _id: user._id
        }, {
          $set: {
            'emails.0.address': user.emails[0].address.toLowerCase()
          }
        });
      });
    }
  });
  </code></pre>
  
  <p>Which should go through, find any names with an upper case letter, and update the email address to a lower case version.  It doesn't work, it throws an index error in the MongoDB driver:</p>
  
  <pre><code>I20150904-01:30:23.322(-7)? Exception while invoking method 'normalizeEmails' MongoError: E11000 duplicate key error index: meteor.users.$emails.address_1  dup key: { : "usedtobetaller@fixemandhow.com" }
  I20150904-01:30:23.322(-7)?     at Object.Future.wait (/home/mowgli/.meteor/packages/meteor-tool/.1.1.4.70jrul++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15)
  I20150904-01:30:23.323(-7)?     at [object Object].&lt;anonymous&gt; (packages/meteor/helpers.js:119:1)
  I20150904-01:30:23.323(-7)?     at [object Object].MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:678:1)
  I20150904-01:30:23.323(-7)?     at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/mongo/collection.js:575:1)
  I20150904-01:30:23.323(-7)?     at server/main.invitePub.coffee:320:22
  I20150904-01:30:23.323(-7)?     at packages/mongo/mongo_driver.js:974:1
  I20150904-01:30:23.323(-7)?     at [object Object]._.extend.forEach (packages/mongo/mongo_driver.js:965:1)
  I20150904-01:30:23.323(-7)?     at [object Object]._.extend.map (packages/mongo/mongo_driver.js:973:1)
  I20150904-01:30:23.323(-7)?     at [object Object].Cursor.(anonymous function) [as map] (packages/mongo/mongo_driver.js:812:1)
  I20150904-01:30:23.324(-7)?     at [object Object].Meteor.methods.normalizeEmails (server/main.invites.coffee:317:56)
  I20150904-01:30:23.324(-7)?     - - - - -
  I20150904-01:30:23.324(-7)?     at Object.toError (/home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/utils.js:114:11)
  I20150904-01:30:23.324(-7)?     at /home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/collection/core.js:577:27
  I20150904-01:30:23.324(-7)?     at /home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/db.js:1195:7
  I20150904-01:30:23.324(-7)?     at /home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/db.js:1903:9
  I20150904-01:30:23.324(-7)?     at Server.Base._callHandler (/home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
  I20150904-01:30:23.324(-7)?     at /home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/server.js:487:18
  I20150904-01:30:23.324(-7)?     at [object Object].MongoReply.parseBody (/home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
  I20150904-01:30:23.325(-7)?     at [object Object].&lt;anonymous&gt; (/home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/server.js:445:20)
  I20150904-01:30:23.325(-7)?     at [object Object].emit (events.js:95:17)
  I20150904-01:30:23.325(-7)?     at [object Object].&lt;anonymous&gt; (/home/mowgli/.meteor/packages/mongo/.1.1.0.zb7oxo++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)
  </code></pre>
  
  <h3>EDIT:</h3>
  
  <p>Apparently I have duplicate emails, so Accounts-UI emails are case sensitive. It's entirely possible to end up with three users bob@email.com, Bob@email.com, and BOB@email.com.  </p>
  
  <p>I'm leaving this up as a cautionary tale and a simple example of updating email  addresses in Meteor</p>