siyuan-note/siyuan · error

first argument must be method name string

Error message

first argument must be method name string

What it means

Thrown by siyuan.rpc.bind when the first argument is not a string. The RPC method name must be a string so it can be looked up by callers.

Source

Thrown at kernel/plugin/api_rpc.go:54

	rpc := rt.NewObject()

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

		var argErr error
		var name string
		var method goja.Callable
		var descriptions []string
		if len(call.Arguments) < 2 {
			argErr = fmt.Errorf("method name and function required")
		} else {
			nameArg := call.Argument(0)
			methodArg := call.Argument(1)
			descArgs := call.Arguments[2:]
			if goja.IsString(nameArg) {
				name = nameArg.String()
			} else {
				argErr = fmt.Errorf("first argument must be method name string")
			}
			if argErr == nil {
				if methodJs, ok := goja.AssertFunction(methodArg); ok {
					method = methodJs
				} else {
					argErr = fmt.Errorf("second argument must be a function")
				}
			}
			if argErr == nil {
				descriptions = make([]string, len(descArgs))
				for i, a := range descArgs {
					descriptions[i] = a.String()
				}
			}
		}

		runErr := p.worker.Run(func(rt *goja.Runtime) (result any, err error) {
			if argErr != nil {

View on GitHub (pinned to 251596fc0d)

Solutions

  1. Pass the method name as a string: siyuan.rpc.bind('add', fn).
  2. Coerce: siyuan.rpc.bind(String(name), fn).

Example fix

// before
siyuan.rpc.bind(methodId, fn);
// after
siyuan.rpc.bind('add', fn);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof name !== 'string') throw new TypeError('name must be string');
siyuan.rpc.bind(name, fn);

Type guard

const isMethodName = (v) => typeof v === 'string' && v.length > 0;

Prevention

When it happens

Trigger: Calling bind(123, fn), bind({name:'x'}, fn), or bind(undefined, fn). goja.IsString(nameArg) is false at api_rpc.go:51.

Common situations: Plugin builds the name from a non-string constant, or passes an object expecting destructuring.

Related errors


AI-assisted analysis of siyuan-note/siyuan@251596fc0d (2026-08-12). Data as JSON: /api/errors/735951fa6c77a0bf. Report an issue: GitHub.