【发布时间】: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