Javascript get.json不是Promise.all w/fetch上的函数

Javascript get.json不是Promise.all w/fetch上的函数,javascript,fetch,promise.all,Javascript,Fetch,Promise.all,哦,我又一次有了这些承诺。all blues:(我有一个函数,它从提供的URL生成一个获取调用数组,然后我们希望通过一个承诺来检索数据。all和REPONSE返回数组,或者更好,只是将承诺返回给调用函数。问题是,这会导致错误w/控制台显示: There was problem retrieving data. TypeError: r.json is not a function 该功能的代码为: const getLeagueLeaders = (url, params) => {

哦,我又一次有了这些承诺。all blues:(我有一个函数,它从提供的URL生成一个获取调用数组,然后我们希望通过一个承诺来检索数据。all和REPONSE返回数组,或者更好,只是将承诺返回给调用函数。问题是,这会导致错误w/控制台显示:

There was problem retrieving data. TypeError: r.json is not a function
该功能的代码为:

 const getLeagueLeaders = (url, params) => {
  // First let's create the array of url's
  let queryURLs = [];

  params.forEach((param) => {
    queryURLs.push(
      fetch(`${url}${new URLSearchParams(param)}`, {
        method: "get",
        headers: {
          Authorization:
            "Basic ==",
        },
      }).then((res) => res.json())
    );
  });

  return (
    Promise.all(queryURLs)
      // map array of responses into an array of response.json() to read their content
      .then((responses) => responses.map((r) => r.json()))
      .catch((err) => {
        console.error("There was problem retrieving data.", err);
      })
  );
};
    
    module.exports = getLeagueLeaders;
和在Vue组件中

 mounted: async function () {
        const leagueLeadersResponseArray = await getLeagueLeaders(
          this.fetchBaseUrl,
          this.params
        );
this.qbLeaders =
      leagueLeadersResponseArray[0].cumulativeplayerstats.playerstatsentry;
显然leagueLeadersResponseArray是未定义的。我研究了.json(),没有发现我是如何错误地使用它的。起初我认为我需要一个Promise.all包装器来响应.map((r)=>r.json()),但这也没有什么好处。我看了这个,但我没有像他那样使用fetch。非常感谢任何指导

其他人的更新工作代码:

// ---------- src/js/modules/ ------------------ //

/* jshint ignore:start */
// Make function to retrieve League Leaders in a Category

const getLeagueLeaders = (url, params) => {
  // First let's create the array of url's
  let queryURLs = [];

  params.forEach((param) => {
    queryURLs.push(
      fetch(`${url}${new URLSearchParams(param)}`, {
        method: "get",
        headers: {
          Authorization:
            "Basic ==",
        },
      }).then((res) => res.json())
    );
  });

  return Promise.all(queryURLs).catch((err) => {
    console.error("There was problem retrieving data.", err);
  });
};

module.exports = getLeagueLeaders;

模板字符串位于整个
fetch
中,而它只应位于要获取的参数中:

params.forEach((param)=>{
push(fetch(`${url}${new URLSearchParams(param)}'){
方法:“获取”,
标题:{
授权:
“基本******=”,
}
}));
});
然后,您有一个
.Then(data=>{return data})
,它不做任何事情,因为
return
然后
回调返回,而不是函数。您应该返回
promise.all
给您的承诺:

return Promise.all(queryURLs)
//将响应数组映射到response.json()数组中以读取其内容
.then((responses)=>responses.map((r)=>r.json())//获取错误检索数据时出现问题。TypeError:r.json不是函数
.catch((错误)=>{
错误(“检索数据时出现问题。”,err);
});

fetch()
被视为字符串模板的一部分,而不是您想要的JavaScript函数。字符串没有函数
.json()
。请调用实际的
fetch
函数,而不要像
fetch(`your string literal URL`,{/*your request config object*/})那样用反勾号将其括起来
。另外,
然后(数据=>{returndata})
什么都不做。你需要
返回承诺。all
取而代之,它返回一个承诺数组,该数组将一个接一个
承诺。all
。字符串没有
.JSON
方法-检查
queryURLs
数组的内容!我如何使用返回的数据。我认为。qbLeaders=leagueLeadersResponseArray[0]。cumulativeplayerstats.playerstatsentry;将允许我访问API的第一个响应。但我发现它未定义。您应该
console.log(leagueLeadersResponseArray)
以确保数据符合您的预期。现在我很困惑。我以为一旦承诺。一切都解决了,那么leagueLeadersResponseArray将以每个文档的数组形式在其中包含我的三个响应。取而代之的是:这是return leagueLeaders数组[对象承诺],[对象承诺],[对象承诺]。任何建议。噢,您可能希望将
queryURLs.push(fetch(…)
更改为
queryURLs.push(fetch(…)。然后((res)=>res.json())
就可以了。请参阅更新的代码。现在我们回到检索数据时出现问题的地方。类型错误:r.json不是函数错误???