mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
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:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ SELECT h."data"
|
||||
};
|
||||
|
||||
module.getObjectField = async function (key, field) {
|
||||
if (!key) {
|
||||
if (!key || !field) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.getObjectField = async function (key, field) {
|
||||
if (!key) {
|
||||
if (!key || !field) {
|
||||
return null;
|
||||
}
|
||||
const cachedData = {};
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user