convert NaN uids to 0

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 0ea89c2799
commit 5a55c882ab

@ -31,6 +31,10 @@ module.exports = function (User) {
return callback(null, []); return callback(null, []);
} }
uids = uids.map(function (uid) {
return isNaN(uid) ? 0 : uid;
});
var fieldsToRemove = []; var fieldsToRemove = [];
function addField(field) { function addField(field) {
if (fields.indexOf(field) === -1) { 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'); fields.push('uid');
} }
@ -58,7 +62,11 @@ module.exports = function (User) {
async.waterfall([ async.waterfall([
function (next) { 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) { function (users, next) {
users = uidsToUsers(uids, uniqueUids, users); users = uidsToUsers(uids, uniqueUids, users);
@ -80,24 +88,7 @@ module.exports = function (User) {
}; };
User.getUsersData = function (uids, callback) { User.getUsersData = function (uids, callback) {
if (!Array.isArray(uids) || !uids.length) { User.getUsersFields(uids, [], callback);
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);
}; };
function uidsToUsers(uids, uniqueUids, usersData) { function uidsToUsers(uids, uniqueUids, usersData) {

@ -194,8 +194,11 @@ describe('Hash methods', function () {
it('should return undefined for all fields if object does not exist', function (done) { it('should return undefined for all fields if object does not exist', function (done) {
db.getObjectsFields(['doesnotexist1', 'doesnotexist2'], ['name', 'age'], function (err, data) { db.getObjectsFields(['doesnotexist1', 'doesnotexist2'], ['name', 'age'], function (err, data) {
assert.ifError(err); assert.ifError(err);
assert.equal(data.name, null); assert(Array.isArray(data));
assert.equal(data.age, null); 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(); done();
}); });
}); });

@ -441,6 +441,16 @@ describe('User', function () {
done(); 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 () { describe('not logged in', function () {

Loading…
Cancel
Save