将Probot事件函数重构为单独的文件会导致错误:TypeError:处理程序不是函数

问题描述

我有docs中的香草probot事件函数,它对新问题进行了评论

const probotApp = app => {
  app.on("issues.opened",async context => {
    const params = context.issue({ body: "Hello World!" });
    return context.github.issues.createComment(params);
  });
}

这很好。

我将代码重构到一个单独的文件

index.js

const { createComment } = require("./src/event/probot.event");

const probotApp = app => {
  app.on("issues.opened",createComment);
}

probot.event.js

module.exports.createComment = async context => {
  const params = context.issue({ body: "Hello World!" });
  return context.github.issues.createComment(params);
};

但是我收到此错误

ERROR (event): handler is not a function
    TypeError: handler is not a function
        at C:\Users\User\probot\node_modules\@octokit\webhooks\dist-node\index.js:103:14
        at processticksAndRejections (internal/process/task_queues.js:97:5)
        at async Promise.all (index 0)

当我使用夹具创建测试as recommended in the docs并使用nock模拟事件webhook调用时,此方法可以正常工作。但是当我在GitHub上创建一个真正的问题时,就会抛出此错误

如何在不引起错误的情况下将代码重构到一个单独的文件中?

解决方法

这是我的错误。

这是整个probot.event.js文件:

module.exports.createComment = async context => {
  const params = context.issue({ body: "Hello World!" });
  return context.github.issues.createComment(params);
};


module.exports = app => {
  // some other event definitions
}

通过定义module.exports = app,我重写了先前的module.export。因此,createComment函数从未导出。

移除module.exports = app = { ... }可以解决问题!