siyuan-note/siyuan · error

document sort item must not be nil

Error message

document sort item must not be nil

What it means

Document sort planning requires every entry of docSorts to be a non-nil item; a nil entry aborts with errors.New("document sort item must not be nil"). Like the notebook check (error 1393), it is a request-shape guard ensuring the payload contains only concrete sort items before any tree mutation.

Solutions

  1. Strip null/undefined entries from the docSorts array before the request
  2. Initialize every element of the client-side array before serialization
  3. Validate the payload schema client-side prior to sending

Example fix

// before: JS array with holes
const sorts = new Array(n); sorts[0] = {id: "..."};
sortDocs(sorts)
// after
const sorts = docs.map(d => ({id: d.id}));
sortDocs(sorts.filter(Boolean))
Defensive patterns

Strategy: validation

Validate before calling

if (docSorts.some(i => i == null)) throw new Error('docSorts contains null entries');

Type guard

const isDocSortItem = (i) => i != null && typeof i.id === 'string';

Try / catch

try { await api.sortDocs(docSorts); } catch (e) { if (String(e).includes('must not be nil')) { await api.sortDocs(docSorts.filter(Boolean)); } else { throw e; } }

Prevention

When it happens

Trigger: Posting the document sort API with null elements inside the docSorts array — unassigned Go slice slots, JSON nulls from undefined array positions, or template-generated payloads with empty optional entries.

Common situations: Plugin builds make([]*DocSortItem, n) and fills only some indexes; frontend state array containing nulls after removals without compaction; scripting languages serializing undefined as null.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/41bdf4c441371a99. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/file.go:2927

			return ret, errors.New("notebook sort item must not be nil")
		}
		if _, ok := notebookIDs[item.ID]; ok {
			return ret, fmt.Errorf("duplicate notebook ID [%s]", item.ID)
		}
		notebookIDs[item.ID] = struct{}{}

		box := boxes[item.ID]
		if nil == box {
			return ret, fmt.Errorf("notebook [%s] not found", item.ID)
		}
		notebookPlans = append(notebookPlans, &notebookSortPlan{item: item, box: box})
	}

	docPlans := make([]*docSortPlan, 0, len(docSorts))
	docIDs := map[string]struct{}{}
	for _, item := range docSorts {
		if nil == item {
			return ret, errors.New("document sort item must not be nil")
		}
		if _, ok := docIDs[item.ID]; ok {
			return ret, fmt.Errorf("duplicate document ID [%s]", item.ID)
		}
		docIDs[item.ID] = struct{}{}

		bt := treenode.GetBlockTree(item.ID)
		if nil == bt || nil == openedBoxes[bt.BoxID] {
			return ret, fmt.Errorf("document [%s] not found in opened and unlocked notebooks", item.ID)
		}
		if bt.ID != bt.RootID || "d" != bt.Type || IsBoxDoc(bt.BoxID, bt.RootID) {
			return ret, fmt.Errorf("block [%s] is not a sortable document", item.ID)
		}
		if nil == boxes[bt.BoxID] {
			return ret, fmt.Errorf("notebook [%s] not found for document [%s]", bt.BoxID, item.ID)
		}
		docPlans = append(docPlans, &docSortPlan{item: item, boxID: bt.BoxID, parentPath: path.Dir(bt.Path)})
	}

View on GitHub (pinned to 9f775e8a12)