siyuan-note/siyuan · error

attribute view item markdown is nil

Error message

attribute view item markdown is nil

What it means

CreateAttributeViewItemWithMarkdown creates a database item bound to a new document generated from the given Markdown. The document parameter is a pointer, and the function rejects a nil pointer outright because there is no Markdown content to create the bound document from.

Solutions

  1. Pass a non-nil *CreateAttributeViewItemMarkdown with valid Markdown content
  2. If no bound document is needed, call CreateAttributeViewItem instead
  3. Guard the call site: construct the document struct before invoking, and fall back to CreateAttributeViewItem when no content is available

Example fix

// before
res, err := CreateAttributeViewItemWithMarkdown(avID, blockID, viewID, tplID, prevID, groupID, nil)
// after
var doc *CreateAttributeViewItemMarkdown
if md != "" {
    doc = &CreateAttributeViewItemMarkdown{Markdown: md}
}
res, err := CreateAttributeViewItemWithMarkdown(avID, blockID, viewID, tplID, prevID, groupID, doc)
Defensive patterns

Strategy: type-guard

Validate before calling

if (!document || typeof document.markdown !== 'string') {
  return CreateAttributeViewItem(avID, blockID, viewID, templateID, previousID, groupID);
}

Type guard

function hasMarkdownDocument(doc) {
  return doc !== null && doc !== undefined && typeof doc === 'object';
}

Try / catch

try {
  return await api.createAttributeViewItemWithMarkdown({avID, blockID, document});
} catch (e) {
  if (e.message.includes('item markdown is nil')) {
    return await api.createAttributeViewItem({avID, blockID}); // detached item
  }
}

Prevention

When it happens

Trigger: Calling CreateAttributeViewItemWithMarkdown with the document argument explicitly nil (or a nil *CreateAttributeViewItemMarkdown variable passed through).

Common situations: Callers that only need a detached item mistakenly using the WithMarkdown variant; a markdown value built conditionally that ended up nil; wrappers that forward nil when no template content was loaded.

Related errors


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

Appendix: source

Thrown at kernel/model/attribute_view_new_item.go:89

// CreateAttributeViewItemDocsResult 描述将游离条目批量转换为文档后的结果。
type CreateAttributeViewItemDocsResult struct {
	ItemIDs        []string     `json:"itemIDs"`
	BlockIDs       []string     `json:"blockIDs"`
	SkippedItemIDs []string     `json:"skippedItemIDs,omitempty"`
	Warnings       []string     `json:"warnings,omitempty"`
	Transaction    *Transaction `json:"-"`
}

// CreateAttributeViewItem 按指定模板创建一个数据库条目。templateID 为空时创建空白游离条目。
func CreateAttributeViewItem(avID, blockID, viewID, templateID, previousID, groupID string) (*CreateAttributeViewItemResult, error) {
	return createAttributeViewItem(avID, blockID, viewID, templateID, previousID, groupID, nil)
}

// CreateAttributeViewItemWithMarkdown 按指定的文档类型模板创建数据库条目,并使用传入的 Markdown 创建绑定文档。
func CreateAttributeViewItemWithMarkdown(avID, blockID, viewID, templateID, previousID, groupID string,
	document *CreateAttributeViewItemMarkdown) (*CreateAttributeViewItemResult, error) {
	if nil == document {
		return nil, errors.New("attribute view item markdown is nil")
	}
	return createAttributeViewItem(avID, blockID, viewID, templateID, previousID, groupID, document)
}

func createAttributeViewItem(avID, blockID, viewID, templateID, previousID, groupID string,
	document *CreateAttributeViewItemMarkdown) (*CreateAttributeViewItemResult, error) {
	attrView, err := avParseView(avID, blockID)
	if nil != err {
		return nil, err
	}
	createdAt := time.Now()
	itemTemplate := attrView.GetNewItemTemplate(templateID)
	var prunedOptions []*av.PrunedNewItemTemplateOption
	if "" != templateID && nil == itemTemplate {
		return nil, fmt.Errorf("new item template [%s] not found", templateID)
	}
	if nil == itemTemplate {
		itemTemplate = &av.NewItemTemplate{TargetType: av.NewItemTargetDetached}

View on GitHub (pinned to 9f775e8a12)