为什么我的promise会返回[object]或[array]的数组

问题描述 投票:0回答:1

我试图从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],
javascript node.js promise async-await
1个回答
0
投票

这就是控制台如何显示它。在你的“我期待的东西”中,你给出了一个对象数组的例子。

控制台输出显示了一个对象数组,但没有给出每个对象的详细信息。

如果你想强制控制台显示每个info对象中的内容,你可以使用console.log(name, JSON.stringify(info));

顺便说一句,你的代码末尾附近的return theStuff看起来不合适。不确定那是做什么的。

© www.soinside.com 2019 - 2024. All rights reserved.