Skip to content

Node.js based forum software built for the modern web

convert NaN uids to 0

Barış Soner Uşaklı5a55c882ab8e
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 () {