我今天使用Meteor.js和Accounts-ui-bootstrap3包解决了很多用户错误。到目前为止,我最喜欢的错误是密码重置时的“令牌已过期”,但是当输入正确的电子邮件地址时,真正导致问题的是“用户未找到”。需要一段时间才能解决问题,但问题是由所有大写字母中的电子邮件地址创建的帐户引起的。显然,帐户对电子邮件地址区分大小写。
现在,虽然我已经将所有代码更新为小写传入的用户电子邮件,但我仍然需要修复现有用户。我创建了一个服务器方法来处理这个问题:
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()
}
});
});
}
});
应该检查哪些内容,查找带有大写字母的任何名称,并将电子邮件地址更新为小写版本。它不起作用,它会在MongoDB驱动程序中引发索引错误:
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].<anonymous> (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].<anonymous> (/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].<anonymous> (/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)
显然我有重复的电子邮件,因此Accounts-UI电子邮件区分大小写。完全有可能最终有三个用户bob @ email.com,Bob @ email.com和BOB@email.com。
我将此作为一个警示故事和一个更新Meteor中电子邮件地址的简单示例
答案 0 :(得分:1)
对于这个问题,这是一个小硬核,但是这段代码会使任何帐户无法通过电子邮件地址中的大写创建(即使在服务器上):
if (Meteor.isServer) {
Accounts.validateNewUser(function(user) {
if (user.emails[0].address.match(/[A-Z]{1,}/) {
throw new Meteor.Error(403, "Emails may only be lowercase.");
return false;
}
return true;
});
}
答案 1 :(得分:0)
我做了一些测试和帐户包,不允许您使用相同的电子邮件创建两个用户(它不区分大小写)。
我尝试过创建两个用户:&#34; alex@email.com"和#34; Alex@email.com" 在第二次尝试中,我得到:&#34;电子邮件已经存在。&#34;
我认为您的mongo搜索不正确:
Meteor.users.find({
'emails.address': /[A-Z]{1,}/
})
&#39; emails.address&#39;不是用于搜索的有效密钥,电子邮件&#39;是一个数组。
我认为正确的语法是:
Meteor.users.find({
emails: {
$elemMatch: {
address: /[A-Z]{1,}/
}
}
})