fix(isEmptyOrWhitespace): avoid exception throwing when passed value is undefined

the req.body value from "routes/api/branches" actually seems to never get parsed into a JS object, but arrives as text string, so req.body.prefix could be undefined, which of course would cause an error to be thrown, when trying to call "match" on undefined.
This commit is contained in:
Panagiotis Papadopoulos 2025-01-29 17:07:48 +01:00
parent 8546fe2333
commit 03c1128a72
2 changed files with 4 additions and 2 deletions

View File

@ -216,6 +216,7 @@ function deleteBranch(req: Request) {
function setPrefix(req: Request) { function setPrefix(req: Request) {
const branchId = req.params.branchId; const branchId = req.params.branchId;
//TriliumNextTODO: req.body arrives as string, so req.body.prefix will be undefined did the code below ever even work?
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix; const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
const branch = becca.getBranchOrThrow(branchId); const branch = becca.getBranchOrThrow(branchId);

View File

@ -71,8 +71,9 @@ export function hash(text: string) {
return crypto.createHash("sha1").update(text).digest("base64"); return crypto.createHash("sha1").update(text).digest("base64");
} }
export function isEmptyOrWhitespace(str: string) { export function isEmptyOrWhitespace(str: string | null | undefined) {
return str === null || str.match(/^ *$/) !== null; if (!str) return true;
return str.match(/^ *$/) !== null;
} }
export function sanitizeSqlIdentifier(str: string) { export function sanitizeSqlIdentifier(str: string) {