siyuan-note/siyuan · error

topic and event required

Error message

topic and event required

What it means

The siyuan.event emit/publish call requires exactly a topic string and an event payload. When fewer than two arguments are supplied, the call is rejected synchronously and the returned promise rejects with 'topic and event required'. This is strict arity validation before any kernel work is dispatched.

Solutions

  1. Pass both arguments: emit('topic', data)
  2. Pass an explicit empty payload (e.g. {}) when there is no data
  3. Check any spread call emit(...args) actually yields two elements
  4. Verify argument order: topic first, payload second

Example fix

// before
await siyuan.event.publish('my-topic');
// after
await siyuan.event.publish('my-topic', {detail: 'hello'});
Defensive patterns

Strategy: validation

Validate before calling

function assertPublishArgs(args) {
  if (!Array.isArray(args) || args.length < 2) {
    throw new TypeError('publish requires (topic, event) — two arguments');
  }
  return args;
}
// call site: siyuan.event.publish(...assertPublishArgs([topic, payload]))

Type guard

null

Try / catch

try {
  await siyuan.event.publish(topic, payload);
} catch (e) {
  if (String(e.message).includes('topic and event required')) {
    console.error('publish called without a payload; topic =', topic);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling the event function with zero or one argument, e.g. emit('my-topic') with no payload, or emit() with none; spreading an array that has fewer than two elements.

Common situations: Assuming an optional/empty default payload; variables named for the payload that ended up undefined and dropped; refactors that removed the data argument but kept the call.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at kernel/plugin/api_event.go:46

func injectEvent(p *KernelPlugin, rt *goja.Runtime, siyuan *goja.Object) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("injectEvent: %v", r)
		}
	}()

	event := rt.NewObject()

	lo.Must0(event.Set("handler", goja.Null()))

	lo.Must0(event.Set("emit", rt.ToValue(func(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
		promise, resolve, reject := rt.NewPromise()

		var argErr error
		var topic string
		var eventData any
		if len(call.Arguments) < 2 {
			argErr = fmt.Errorf("topic and event required")
		} else {
			topic = call.Argument(0).String()
			if topic == "" {
				argErr = fmt.Errorf("topic required")
			} else {
				eventData = call.Argument(1).Export()
			}
		}

		runErr := p.worker.Run(func(rt *goja.Runtime) (result any, err error) {
			if argErr != nil {
				err = argErr
				return
			}
			p.bus.Publish(topic, eventData)
			return
		}, func(rt *goja.Runtime, result any, err error) {
			if lo.IsNil(err) {

View on GitHub (pinned to 9f775e8a12)