【发布时间】:2019-04-27 04:39:57
【问题描述】:
我正在尝试遍历来自 github 的 api 的 url 列表,并返回一个包含所有信息的数组。我返回的是一个带有 [object],[object] 而不是实际数据的数组。我认为这与我的承诺没有解决有关。
这是我的功能:
async function getCommitsByWeek() {
const allRepoNames = await getRepos(user);
const repoURLs = await allRepoNames.map(({ full_name, name }) => {
return {
name,
url: `https://api.github.com/repos/${full_name}/stats/contributors`,
};
});
const theStuff = await Promise.all(
repoURLs.map(({ url, name }) =>
axios.get(url, AUTH).then(({ data }) => {
const info = data[0].weeks.map(week => {
return { w: week.w, c: week.c };
});
//console.log(name, info);
return { name, info };
})
)
);
return theStuff
}
这是预期的
{ name: 'my-github-data_frontend',
info: [ { w: 1555804800, c: 1 } ] }
{ name: 'Below-the-fold',
info:
[ { w: 1542499200, c: 2 },
{ w: 1543104000, c: 0 },
{ w: 1555200000, c: 0 },
{ w: 1555804800, c: 0 } ] }
{ name: 'contraction-app',
info:
[ { w: 1536451200, c: 9 },
{ w: 1537056000, c: 5 },
{ w: 1555200000, c: 0 },
{ w: 1555804800, c: 0 } ] }
这就是我目前得到的
{ name: 'Below-the-fold',
info:
[ [Object],
[Object],
[Object], ] },
{ name: 'contraction-app',
info:
[ [Object],
[Object],
[Object],
[Object],
【问题讨论】:
-
控制台就是这样显示的。将其输出为 JSON 以确定它是什么。
-
尼特:没有理由
await allRepoNames.map这不是一个承诺。其他 nit:在任何地方使用async/await- 你可以用它替换.then
标签: javascript node.js promise async-await