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, []);
}
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) {
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) {

@ -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();
});
});

@ -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 () {

Loading…
Cancel
Save