{"record":{"id":"7f6f597307af00ca","repo":"siyuan-note/siyuan","slug":"sort-target-id-is-required-when-sort-position-is-s","errorCode":null,"errorMessage":"sort target ID is required when sort position is specified","messagePattern":"sort target ID is required when sort position is specified","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/file.go","lineNumber":1308,"sourceCode":"\tcreateDocLock.Lock()\n\tdefer createDocLock.Unlock()\n\n\tbox, err := getOpenedBox(boxID)\n\tif nil != err {\n\t\treturn\n\t}\n\tsortTargetID, _ := arg[\"sortTargetID\"].(string)\n\tsortPosition, _ := arg[\"sortPosition\"].(string)\n\tif \"\" != sortTargetID {\n\t\tif \"before\" != sortPosition && \"after\" != sortPosition {\n\t\t\treturn nil, fmt.Errorf(\"invalid sort position [%s]\", sortPosition)\n\t\t}\n\t\ttarget := treenode.GetBlockTree(sortTargetID)\n\t\tif !isSortableDocument(target) || target.BoxID != boxID || path.Dir(target.Path) != path.Dir(p) {\n\t\t\treturn nil, fmt.Errorf(\"sort target document [%s] is not a sibling of the new document\", sortTargetID)\n\t\t}\n\t} else if \"\" != sortPosition {\n\t\treturn nil, errors.New(\"sort target ID is required when sort position is specified\")\n\t}\n\n\tluteEngine := util.NewLute()\n\tluteEngine.SetHTMLTag2TextMark(true)\n\tdom := luteEngine.Md2BlockDOM(md, false)\n\ttree, err = createDoc0(box.ID, p, title, dom, false, syncWrite)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tFlushTxQueue()\n\tif \"\" != sortTargetID {\n\t\tif _, sortErr := ReorderDocs([]string{tree.ID}, sortTargetID, sortPosition); nil != sortErr {\n\t\t\tlogging.LogErrorf(\"reorder created document [%s] failed: %s\", tree.ID, sortErr)\n\t\t\tbox.setSortByConf(path.Dir(tree.Path), tree.ID)\n\t\t}\n\t} else if 0 < len(sorts) {\n\t\tChangeFileTreeSort(box.ID, sorts)","sourceCodeStart":1290,"sourceCodeEnd":1326,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/file.go#L1290-L1326","documentation":"createDocByMd refuses to create a document when a sortPosition (e.g. 'top', 'bottom', or an index) is given but no sortTargetID identifies the sibling document to position against. SiYuan cannot order the new document without a concrete target document. This is a pure API misuse guard, not a runtime fault.","triggerScenarios":"Calling CreateDocByMd/CreateDocByMdSync (or /api/filetree/createDocWithMd-family endpoints) with a non-empty sortPosition but empty or whitespace sortTargetID.","commonSituations":"Plugin or script code that builds the request payload conditionally and fills sortPosition but forgets sortTargetID; copy-pasted API snippets that hardcode 'sortPosition':'top' without 'sortTargetID'.","solutions":["Pass a valid sibling document ID in sortTargetID whenever sortPosition is set","Omit sortPosition entirely to append the document normally","Verify the target exists and is a sibling first via GetBlockTree / listDocs"],"exampleFix":"// before\nfetchPost('/api/filetree/createDocWithMd', {notebook: boxID, path: p, markdown: md, sortPosition: 'top'})\n// after\nfetchPost('/api/filetree/createDocWithMd', {notebook: boxID, path: p, markdown: md, sortPosition: 'top', sortTargetID: siblingDocID})","handlingStrategy":"validation","validationCode":"if (sortPosition && !sortTargetID) throw new Error('sortTargetID is required when sortPosition is set')","typeGuard":null,"tryCatchPattern":"try { await api.createDocWithMd(args) } catch (e) { if (String(e).includes('sort target ID is required')) { /* retry without sortPosition */ } }","preventionTips":["Treat sortPosition and sortTargetID as a paired parameter set","Build request payloads from a helper that asserts the pairing","Read the API contract for createDocWithMd before use"],"tags":["api","validation","missing-argument"],"backgroundTag":"missing-required-argument","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"}