From 8abcf571e822b41d602f27a8eecf769e470a83c4 Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 16 Aug 2022 23:46:56 +0200 Subject: [PATCH] fixes for undefined parent note #3066 --- src/becca/entities/branch.js | 2 +- src/becca/entities/note.js | 6 ++++-- src/becca/similarity.js | 2 +- src/routes/api/tree.js | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/becca/entities/branch.js b/src/becca/entities/branch.js index c66485edf..44ba60cf3 100644 --- a/src/becca/entities/branch.js +++ b/src/becca/entities/branch.js @@ -105,7 +105,7 @@ class Branch extends AbstractEntity { return this.childNote; } - /** @returns {Note} */ + /** @returns {Note|undefined} - root branch will have undefined parent, all other branches have to have a parent note */ get parentNote() { if (!(this.parentNoteId in this.becca.notes) && this.parentNoteId !== 'none') { // entities can come out of order in sync/import, create skeleton which will be filled later diff --git a/src/becca/entities/note.js b/src/becca/entities/note.js index 40d16bc7e..1e4b9bf3d 100644 --- a/src/becca/entities/note.js +++ b/src/becca/entities/note.js @@ -699,9 +699,11 @@ class Note extends AbstractEntity { sortParents() { this.parentBranches.sort((a, b) => a.branchId.startsWith('virt-') - || a.parentNote.hasInheritableOwnedArchivedLabel() ? 1 : -1); + || a.parentNote?.hasInheritableOwnedArchivedLabel() ? 1 : -1); - this.parents = this.parentBranches.map(branch => branch.parentNote); + this.parents = this.parentBranches + .map(branch => branch.parentNote) + .filter(note => !!note); } /** diff --git a/src/becca/similarity.js b/src/becca/similarity.js index 5d2e5e870..f270960c0 100644 --- a/src/becca/similarity.js +++ b/src/becca/similarity.js @@ -281,7 +281,7 @@ async function findSimilarNotes(noteId) { } function gatherAncestorRewards(note) { - if (ancestorNoteIds.has(note.noteId)) { + if (!note || ancestorNoteIds.has(note.noteId)) { return 0; } diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js index 3894fbce3..95cd9d1c0 100644 --- a/src/routes/api/tree.js +++ b/src/routes/api/tree.js @@ -10,7 +10,7 @@ function getNotesAndBranchesAndAttributes(noteIds) { const collectedBranchIds = new Set(); function collectEntityIds(note) { - if (collectedNoteIds.has(note.noteId)) { + if (!note || collectedNoteIds.has(note.noteId)) { return; }