fix: return null if field is falsy

fixes MongoServerError: FieldPath cannot be constructed with empty string error when getObjectField is called with a falsy value
This commit is contained in:
Barış Soner Uşaklı
2025-08-22 11:06:47 -04:00
parent c16f9d6495
commit 09898b94ec
5 changed files with 21 additions and 4 deletions

View File

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

View File

@@ -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;
}

View File

@@ -153,7 +153,7 @@ SELECT h."data"
};
module.getObjectField = async function (key, field) {
if (!key) {
if (!key || !field) {
return null;
}

View File

@@ -96,7 +96,7 @@ module.exports = function (module) {
};
module.getObjectField = async function (key, field) {
if (!key) {
if (!key || !field) {
return null;
}
const cachedData = {};

View File

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