Skip to content

Node.js based forum software built for the modern web

fix: return null if field is falsy

Barış Soner Uşaklı09898b94ecca
diff --git a/src/categories/index.js b/src/categories/index.js
index 15cabac24e..047de142c8 100644
--- a/src/categories/index.js
+++ b/src/categories/index.js
@@ -85,6 +85,9 @@ Categories.getCategoryById = async function (data) {
};
Categories.getCidByHandle = async function (handle) {
+ if (!handle) {
+ return null;
+ }
let cid = await db.sortedSetScore('categoryhandle:cid', handle);
if (!cid) {
// remote cids
diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js
index 958d5384f3..59ea48e1c9 100644
--- a/src/database/mongo/hash.js
+++ b/src/database/mongo/hash.js
@@ -95,7 +95,7 @@ module.exports = function (module) {
};
module.getObjectField = async function (key, field) {
- if (!key) {
+ if (!key || !field) {
return null;
}
const cachedData = {};
@@ -104,7 +104,11 @@ module.exports = function (module) {
return cachedData[key].hasOwnProperty(field) ? cachedData[key][field] : null;
}
field = helpers.fieldToString(field);
- const item = await module.client.collection('objects').findOne({ _key: key }, { projection: { _id: 0, [field]: 1 } });
+ const item = await module.client.collection('objects').findOne({
+ _key: key,
+ }, {
+ projection: { _id: 0, [field]: 1 },
+ });
if (!item) {
return null;
}
diff --git a/src/database/postgres/hash.js b/src/database/postgres/hash.js
index 5e3a842d22..834d46ec3e 100644
--- a/src/database/postgres/hash.js
+++ b/src/database/postgres/hash.js
@@ -153,7 +153,7 @@ SELECT h."data"
};
module.getObjectField = async function (key, field) {
- if (!key) {
+ if (!key || !field) {
return null;
}
diff --git a/src/database/redis/hash.js b/src/database/redis/hash.js
index c03bff055b..f046e62180 100644
--- a/src/database/redis/hash.js
+++ b/src/database/redis/hash.js
@@ -96,7 +96,7 @@ module.exports = function (module) {
};
module.getObjectField = async function (key, field) {
- if (!key) {
+ if (!key || !field) {
return null;
}
const cachedData = {};
diff --git a/test/database/hash.js b/test/database/hash.js
index 55b75c0df6..f1bace299e 100644
--- a/test/database/hash.js
+++ b/test/database/hash.js
@@ -285,6 +285,16 @@ describe('Hash methods', () => {
});
});
+ it('should return null if field is falsy', async () => {
+ const values = await Promise.all([
+ db.getObjectField('hashTestObject', ''),
+ db.getObjectField('hashTestObject', null),
+ db.getObjectField('hashTestObject', false),
+ db.getObjectField('hashTestObject', undefined),
+ ]);
+ assert.deepStrictEqual(values, [null, null, null, null]);
+ });
+
it('should return null and not error', async () => {
const data = await db.getObjectField('hashTestObject', ['field1', 'field2']);
assert.strictEqual(data, null);