【发布时间】:2018-04-02 03:07:18
【问题描述】:
我正在使用 response 节点库发出一个 http 请求,我正在尝试递归调用它(如果用户在某一天提交,请检查前一天。如果没有,请计算所有天数获得连胜)。
问题在于这条线
const githubResponse = await request(options);
吐出错误
Unexpected token o in JSON at position 1
await request(options) 似乎没有返回我期望的 JSON GitHub API 响应,而是 githubResponse 似乎是我无法使用的对象。我猜我使用 async/await 不正确,但我不知道如何解决它。
async function checkUserCommitForDate(user, date) {
const options = {
url: `https://api.github.com/search/commits?q=author:${user}+author-date:${date}`,
headers: {
'User-Agent': 'request',
'Accept': 'application/vnd.github.cloak-preview'
}
};
const githubResponse = await request(options)
// I get an error on the next line
if (JSON.parse(githubResponse).total_count > 0) {
const previousDaysDate = moment(date).subtract(1, 'day').format('YYYY-MM-DD');
let streakCounter = await checkUserCommitForDate(user, previousDaysDate);
streakCounter++;
console.log('streakCounter', streakCounter);
return streakCounter;
} else {
return 0;
}
}
更新:这似乎不是一个承诺,所以我需要以不同的方式格式化它(作为回调)。当我尝试这个时:
async function checkUserCommitForDate(user, date) {
const options = {
url: `https://api.github.com/search/commits?q=author:${user}+author-date:${date}`,
headers: {
'User-Agent': 'request',
'Accept': 'application/vnd.github.cloak-preview'
}
};
request(options, async function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
if (JSON.parse(body).total_count > 0) {
const previousDaysDate = moment(date).subtract(1, 'day').format('YYYY-MM-DD');
let streakCounter = await checkUserCommitForDate(user, previousDaysDate);
streakCounter++;
console.log('streakCounter', streakCounter);
return streakCounter;
} else {
return 0;
}
});
}
线
let streakCounter = await checkUserCommitForDate(user, previousDaysDate);
成为问题,因为streakCounter 未定义,导致日志NaN。
【问题讨论】:
-
您是否使用
request模块,而不是response?如果是这样,它不会返回一个承诺,而是使用回调。 -
什么是 gitubResponse?你有没有在尝试解析之前 console.log 它?
-
在更新中,当我记录正文时,我得到 JSON,当我解析它时,我得到一个对象,我仍然没有返回数字,因为有一个新问题。
-
我不认为您可以将 await/async 与回调一起使用,它们与 Promises 一起使用。请求包使用回调,这使得 async/await 无法使用,因为您无法从回调中真正返回任何内容。
-
"所以我需要用不同的格式(作为回调)。" - 不,所以你需要为此做出承诺。或者只使用
request-promise包而不是request。
标签: node.js asynchronous recursion async-await node-request