mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
fix: getUserField so that it always returns null
if field doesn't exist
user.getUserField('foo', 'bar') returned undefined vs user.getUserField('1', 'bar') which returned null
This commit is contained in:
@@ -161,7 +161,7 @@ module.exports = function (User) {
|
||||
|
||||
User.getUserField = async function (uid, field) {
|
||||
const user = await User.getUserFields(uid, [field]);
|
||||
return user ? user[field] : null;
|
||||
return user && user.hasOwnProperty(field) ? user[field] : null;
|
||||
};
|
||||
|
||||
User.getUserFields = async function (uid, fields) {
|
||||
|
||||
@@ -680,6 +680,12 @@ describe('User', () => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return null if field or user doesn not exist', async () => {
|
||||
assert.strictEqual(await User.getUserField('1', 'doesnotexist'), null);
|
||||
assert.strictEqual(await User.getUserField('doesnotexistkey', 'doesnotexist'), null);
|
||||
assert.strictEqual(await User.getUserField('0', 'doesnotexist'), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('profile methods', () => {
|
||||
|
||||
Reference in New Issue
Block a user