Skip to content

Build your personal knowledge base with Trilium Notes

fixes for undefined parent note #3066

zadam8abcf571e822
diff --git a/src/becca/entities/branch.js b/src/becca/entities/branch.js
index c66485edfc..44ba60cf35 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 40d16bc7ea..1e4b9bf3dd 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 5d2e5e870c..f270960c04 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 3894fbce32..95cd9d1c0a 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;
}