From 5a55c882ab8e0c231eb30f7107c0f9f2473bf974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 1 Jun 2017 16:17:03 -0400 Subject: [PATCH] convert NaN uids to 0 --- src/user/data.js | 31 +++++++++++-------------------- test/database/hash.js | 7 +++++-- test/user.js | 10 ++++++++++ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/user/data.js b/src/user/data.js index f5f4fd3db7..1342c4c657 100644 --- a/src/user/data.js +++ b/src/user/data.js @@ -31,6 +31,10 @@ module.exports = function (User) { return callback(null, []); } + uids = uids.map(function (uid) { + return isNaN(uid) ? 0 : uid; + }); + var fieldsToRemove = []; function addField(field) { if (fields.indexOf(field) === -1) { @@ -39,7 +43,7 @@ module.exports = function (User) { } } - if (fields.indexOf('uid') === -1) { + if (fields.length && fields.indexOf('uid') === -1) { fields.push('uid'); } @@ -58,7 +62,11 @@ module.exports = function (User) { async.waterfall([ function (next) { - db.getObjectsFields(uidsToUserKeys(uniqueUids), fields, next); + if (fields.length) { + db.getObjectsFields(uidsToUserKeys(uniqueUids), fields, next); + } else { + db.getObjects(uidsToUserKeys(uniqueUids), next); + } }, function (users, next) { users = uidsToUsers(uids, uniqueUids, users); @@ -80,24 +88,7 @@ module.exports = function (User) { }; User.getUsersData = function (uids, callback) { - if (!Array.isArray(uids) || !uids.length) { - return callback(null, []); - } - - var uniqueUids = uids.filter(function (uid, index) { - return index === uids.indexOf(uid); - }); - - async.waterfall([ - function (next) { - db.getObjects(uidsToUserKeys(uniqueUids), next); - }, - function (users, next) { - users = uidsToUsers(uids, uniqueUids, users); - - modifyUserData(users, [], next); - }, - ], callback); + User.getUsersFields(uids, [], callback); }; function uidsToUsers(uids, uniqueUids, usersData) { diff --git a/test/database/hash.js b/test/database/hash.js index c1b12aaed7..7b128d76f1 100644 --- a/test/database/hash.js +++ b/test/database/hash.js @@ -194,8 +194,11 @@ describe('Hash methods', function () { it('should return undefined for all fields if object does not exist', function (done) { db.getObjectsFields(['doesnotexist1', 'doesnotexist2'], ['name', 'age'], function (err, data) { assert.ifError(err); - assert.equal(data.name, null); - assert.equal(data.age, null); + assert(Array.isArray(data)); + assert.equal(data[0].name, null); + assert.equal(data[0].age, null); + assert.equal(data[1].name, null); + assert.equal(data[1].age, null); done(); }); }); diff --git a/test/user.js b/test/user.js index d87b2f5ce6..428a0fc038 100644 --- a/test/user.js +++ b/test/user.js @@ -441,6 +441,16 @@ describe('User', function () { done(); }); }); + + it('should get user data even if one uid is NaN', function (done) { + User.getUsersData([NaN, testUid], function (err, data) { + assert.ifError(err); + assert.equal(data[0], null); + assert(data[1]); + assert.equal(data[1].username, userData.username); + done(); + }); + }); }); describe('not logged in', function () {