{"record":{"id":"dbfe355c7e2ee866","repo":"siyuan-note/siyuan","slug":"createdoctree-definition-must-be-a-list","errorCode":null,"errorMessage":"createDocTree definition must be a list","messagePattern":"createDocTree definition must be a list","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":127,"sourceCode":"\t\treturn nil, err\n\t}\n\tif 0 == len(nodes) {\n\t\treturn nil, errors.New(\"createDocTree requires at least one document\")\n\t}\n\treturn nodes, nil\n}\n\ntype templateDocTreeParseState struct {\n\tcount int\n}\n\nfunc (state *templateDocTreeParseState) parseNodes(value any, depth int) ([]*TemplateDocTreeNode, error) {\n\tif maxTemplateDocTreeDepth < depth {\n\t\treturn nil, fmt.Errorf(\"createDocTree exceeds the maximum depth of %d\", maxTemplateDocTreeDepth)\n\t}\n\tvalues, ok := value.([]any)\n\tif !ok {\n\t\treturn nil, errors.New(\"createDocTree definition must be a list\")\n\t}\n\tif 0 == len(values) {\n\t\treturn nil, errors.New(\"createDocTree document list must not be empty\")\n\t}\n\n\tnodes := make([]*TemplateDocTreeNode, 0, len(values))\n\tfor _, value := range values {\n\t\tdefinition, ok := value.(map[string]any)\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"createDocTree document must be a dictionary\")\n\t\t}\n\t\tfor key := range definition {\n\t\t\tswitch key {\n\t\t\tcase \"title\", \"template\", \"define\", \"children\":\n\t\t\tdefault:\n\t\t\t\treturn nil, fmt.Errorf(\"createDocTree document contains unknown field [%s]\", key)\n\t\t\t}\n\t\t}","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L109-L145","documentation":"parseNodes type-asserts the definition value to []any. Any value that is not a JSON array — a string, number, object, or null — fails this assertion and produces this error. The createDocTree definition must always be a list of document dictionaries at every level.","triggerScenarios":"Calling createDocTree with def as a string (e.g. JSON text not parsed), a single object {\"title\":...} instead of an array wrapping it, or a child field set to a non-array value.","commonSituations":"Client double-encodes the definition (sending a JSON string of JSON); passing one document object directly instead of a one-element array; a children field accidentally set to a dict instead of a list.","solutions":["Wrap a single document in an array: [{\"title\": \"Doc\"}] instead of {\"title\": \"Doc\"}.","Parse JSON on the client before assigning to def; do not send the definition as a string.","Ensure every 'children' value in the definition is an array (possibly empty-free — use non-empty lists at top level)."],"exampleFix":"// before\n{\"def\": {\"title\": \"Doc\"}}\n// after\n{\"def\": [{\"title\": \"Doc\"}]}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isDocTreeDef(def) {\n  return Array.isArray(def) && def.every(n => n !== null && typeof n === \"object\" && !Array.isArray(n));\n}\nif (!isDocTreeDef(def)) throw new Error(\"definition must be a list of document objects\");","tryCatchPattern":"try {\n  await createDocTree(payload);\n} catch (e) {\n  if (String(e).includes(\"must be a list\")) {\n    // re-wrap a single object: def = [def]\n  } else { throw e; }\n}","preventionTips":["Always send the definition as a JSON array, never a single object or string","Do not double-encode JSON (a string containing JSON) when building requests","Ensure every children value is an array of objects"],"tags":["type-mismatch","validation","json"],"backgroundTag":"type-mismatch","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}