YMFE/yapi · error

缺少hookname

Error message

缺少hookname

What it means

bindHook(name, listener) requires a hook name; calling it with an empty/undefined/null name throws this immediately. The hooks registry keys every valid hookname, so a falsy name can never match.

Source

Thrown at server/plugin.js:195

    listener: []
  },

  /**
   * addNoticePlugin(config)
   * 
   * config.weixin = {
   *    title: 'wechat',
   *    hander: (emails, title, content)=> {...}
   * }
   */
  addNotice:{
    type: 'multi',
    listener: []
  }
};

function bindHook(name, listener) {
  if (!name) throw new Error('缺少hookname');
  if (name in hooks === false) {
    throw new Error('不存在的hookname');
  }
  if (hooks[name].type === 'multi') {
    hooks[name].listener.push(listener);
  } else {
    if (typeof hooks[name].listener === 'function') {
      throw new Error('重复绑定singleHook(' + name + '), 请检查');
    }
    hooks[name].listener = listener;
  }
}

/**
 *
 * @param {*} hookname
 * @return promise
 */

View on GitHub (pinned to 59bade3a8a)

Solutions

  1. Pass a valid hook name string as first argument (must be one of the hooks registered in server/plugin.js).
  2. Check the variable supplying the name is populated (log it before binding).
  3. Refer to documented hook names e.g. 'add_router', 'interface_save'.

Example fix

// before
yapi.bindHook(config.hookName, handler); // config.hookName undefined
// after
if (config.hookName) yapi.bindHook(config.hookName, handler);
Defensive patterns

Strategy: validation

Validate before calling

function canBindHook(name) {
  return typeof name === 'string' && name.length > 0;
}
if (canBindHook(hookName)) yapi.bindHook(hookName, handler);

Type guard

function isHookName(name) {
  return typeof name === 'string' && name.trim() !== '';
}

Try / catch

try {
  yapi.bindHook(name, listener);
} catch (e) {
  if (e.message === '缺少hookname') console.error('bindHook called without a name');
  else if (e.message === '不存在的hookname') console.error(`Unknown hook: ${name}`);
  else throw e;
}

Prevention

When it happens

Trigger: Calling yapi.bindHook('', undefined, null) in a plugin's server.js — typically a typo like bindHook(variable) where the variable is unset, or a mistakenly empty string literal.

Common situations: Plugin authors hardcoding wrong constant, config-driven hook names that resolve to empty, copy-paste of bindHook call without filling the first argument.

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 YMFE/yapi@59bade3a8a (2026-08-29). Data as JSON: /api/errors/818b7b1318cefad1. Report an issue: GitHub.