diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js index 5f58dfd93b..452ecf9d6e 100644 --- a/src/database/mongo/hash.js +++ b/src/database/mongo/hash.js @@ -120,7 +120,7 @@ module.exports = function (module) { cache.set(key, cachedData[key]); }); - if (!fields.length) { + if (!Array.isArray(fields) || !fields.length) { return keys.map(key => (cachedData[key] ? { ...cachedData[key] } : null)); } return keys.map((key) => { diff --git a/src/database/postgres/hash.js b/src/database/postgres/hash.js index 696e385bfd..1a733a3518 100644 --- a/src/database/postgres/hash.js +++ b/src/database/postgres/hash.js @@ -142,7 +142,9 @@ SELECT h."data"->>$2::TEXT f if (!key) { return null; } - + if (!Array.isArray(fields) || !fields.length) { + return await module.getObject(key); + } const res = await module.pool.query({ name: 'getObjectFields', text: ` @@ -174,7 +176,8 @@ SELECT (SELECT jsonb_object_agg(f, d."value") if (!Array.isArray(keys) || !keys.length) { return []; } - if (!fields.length) { + + if (!Array.isArray(fields) || !fields.length) { return await module.getObjects(keys); } const res = await module.pool.query({ diff --git a/test/database/hash.js b/test/database/hash.js index e1ccd782af..9ac9b3a40f 100644 --- a/test/database/hash.js +++ b/test/database/hash.js @@ -324,6 +324,15 @@ describe('Hash methods', () => { assert.strictEqual(Number(objects[1].age), 3); assert.strictEqual(!!objects[2], false); }); + + it('should return objects if fields is not an array', async () => { + const objects = await db.getObjectsFields(['testObject8', 'testObject9', 'doesnotexist'], undefined); + assert.strictEqual(objects[0].name, 'baris'); + assert.strictEqual(Number(objects[0].age), 99); + assert.strictEqual(objects[1].name, 'ginger'); + assert.strictEqual(Number(objects[1].age), 3); + assert.strictEqual(!!objects[2], false); + }); }); describe('getObjectKeys()', () => {