diff --git a/app/imports/api/library/LibraryNodes.js b/app/imports/api/library/LibraryNodes.js index b31333ca..574ea464 100644 --- a/app/imports/api/library/LibraryNodes.js +++ b/app/imports/api/library/LibraryNodes.js @@ -158,16 +158,19 @@ const insertNode = new ValidatedMethod({ const parentDoc = fetchDocByRef(parentRef); // Check permission to edit - let root; + let rootLibrary; if (parentRef.collection === 'libraries') { - root = parentDoc; + rootLibrary = parentDoc; } else if (parentRef.collection === 'libraryNodes') { - root = Libraries.findOne(parentDoc.root.id); + rootLibrary = Libraries.findOne(parentDoc.root.id); libraryNode.parentId = parentRef.id; } else { throw `${parentRef.collection} is not a valid parent collection` } - assertEditPermission(root, this.userId); + assertEditPermission(rootLibrary, this.userId); + + // Set the root of the node we are inserting + libraryNode.root = { collection: 'libraries', id: rootLibrary._id }; // Remove its ID if it came with one to force a random one to be generated // server-side @@ -183,7 +186,7 @@ const insertNode = new ValidatedMethod({ } // Tree structure changed by insert, reorder the tree - rebuildNestedSets(LibraryNodes, root._id); + rebuildNestedSets(LibraryNodes, rootLibrary._id); // Return the id of the inserted node return nodeId;