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:
Barış Soner Uşaklı
2025-02-11 13:28:25 -05:00
parent d590c2afcf
commit e85662a536
2 changed files with 7 additions and 1 deletions

View File

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

View File

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