【问题标题】:callback failing even though GitHub Installation access Token gets generated即使生成了 GitHub 安装访问令牌,回调也会失败
【发布时间】:2018-12-11 20:50:22
【问题描述】:

我有此代码用于生成安装访问令牌:

gitInstallationAccessToken.getAccessTokensUrl(jwt, function(appAccessTokensUrl) {

  var instance = axios({
    method: "POST",
    url: appAccessTokensUrl,
    headers: {
      "Accept" : "application/vnd.github.machine-man-preview+json",
      "Authorization" : `Bearer ${jwt}`
    }
  })
  .then(function(response) {
    var installationAccessToken = response.data.token;
    console.log(`Installation Access Token: ${installationAccessToken}`)
    callback(installationAccessToken);
  })
  .catch(function(error) {
    console.warn("Unable to authenticate");
    // The request was made and the server responded with a status code
    // that falls out of the range of 2xx
    if (error.response) {
      console.warn(`Status ${error.response.status}`);
      console.warn(`${error.response.data.message}`);
    }
  });
});

它输出Unable to authenticate 所以在某些时候失败了。我的问题是 console.log('Installation Access Token: ${installationAccessToken}') 输出一个令牌,所以我希望回调成功。有什么原因可能会失败吗?

其他信息

这是 catch 中返回的实际错误:

ReferenceError: regeneratorRuntime is not defined
    at eval (webpack:///./lib/githubService.js?:17:51)
    at Object.retrieveIssues (webpack:///./lib/githubService.js?:87:6)
    at eval (webpack:///./lib/getPublicGitHubIssues.js?:78:20)
    at eval (webpack:///./lib/gitInstallationAccessToken.js?:84:9)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

看起来这可能与this 的解决方案 2 有关。我不确定最后两个步骤会去哪里。

【问题讨论】:

  • 您的callback 可能会抛出错误。你应该摆脱它并返回一个承诺。
  • 实际打印的 catch 中的错误是什么?正如其他人所提到的,您在其他地方遇到了错误,因为 .then 在 .catch 之前,catch 将完成它的工作并从上游捕获任何东西。
  • 是的,你是对的。我已将错误添加到问题中。

标签: javascript node.js github github-api referenceerror


【解决方案1】:

我安装了babel-plugin-transform-regenerator 并将import 'babel-polyfill' 添加到问题文件的顶部。似乎已经解决了这个问题,尽管它对我来说没有任何意义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2011-05-11
    • 2018-05-24
    • 2016-09-09
    相关资源
    最近更新 更多