diff --git a/src/database/mongo/sorted/union.js b/src/database/mongo/sorted/union.js index ea4ad4d8e1..f518fab79f 100644 --- a/src/database/mongo/sorted/union.js +++ b/src/database/mongo/sorted/union.js @@ -26,7 +26,7 @@ module.exports = function (module) { async function getSortedSetUnion(params) { if (!Array.isArray(params.sets) || !params.sets.length) { - return; + return []; } let limit = params.stop - params.start + 1; if (limit <= 0) { diff --git a/src/database/postgres/sorted/union.js b/src/database/postgres/sorted/union.js index a8fcbd0d0d..9d671ceb0f 100644 --- a/src/database/postgres/sorted/union.js +++ b/src/database/postgres/sorted/union.js @@ -32,6 +32,9 @@ SELECT COUNT(DISTINCT z."value") c async function getSortedSetUnion(params) { const { sets } = params; + if (!sets || !sets.length) { + return []; + } const start = params.hasOwnProperty('start') ? params.start : 0; const stop = params.hasOwnProperty('stop') ? params.stop : -1; let weights = params.weights || []; diff --git a/src/topics/tags.js b/src/topics/tags.js index 655c18345f..314b5cfd84 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -287,7 +287,7 @@ module.exports = function (Topics) { } Topics.getTagData = async function (tags) { - if (!tags.length) { + if (!tags || !tags.length) { return []; } tags.forEach((tag) => { diff --git a/test/database/sorted.js b/test/database/sorted.js index 842b91b146..a5262a5394 100644 --- a/test/database/sorted.js +++ b/test/database/sorted.js @@ -996,6 +996,11 @@ describe('Sorted Set methods', () => { done(); }); }); + + it('should return empty array if sets is empty', async () => { + const result = await db.getSortedSetRevUnion({ sets: [], start: 0, stop: -1 }); + assert.deepStrictEqual(result, []); + }); }); describe('sortedSetIncrBy()', () => {