Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 我正在尝试使用react从Api获取数据,一个Api依赖于;id";第二个';这是一个,_Reactjs_Api_Async Await_Axios_Fetch - Fatal编程技术网

Reactjs 我正在尝试使用react从Api获取数据,一个Api依赖于;id";第二个';这是一个,

Reactjs 我正在尝试使用react从Api获取数据,一个Api依赖于;id";第二个';这是一个,,reactjs,api,async-await,axios,fetch,Reactjs,Api,Async Await,Axios,Fetch,这是我的代码,第一次抓取返回良好的数据,但第二次抓取只带来承诺 const data = await axios.get( `${API_URL}/${id}` ); return data.data; }; const fetchData = async () => { setLoading(true); const res = await axios.get("${API_URL}"); setData(

这是我的代码,第一次抓取返回良好的数据,但第二次抓取只带来承诺

    const data = await axios.get(
      `${API_URL}/${id}`
    );
    return data.data;
  };
  const fetchData = async () => {
    setLoading(true);
    const res = await axios.get("${API_URL}");
    setData(
        res.data.map(el => {
          const fromApi = fetchIncomes(el.id);
          return {
            ...el,
            ...fromApi
          };
        })
      )
    setLoading(false);
  };

您需要等待
返回的承诺

试试这个:

const fetchIncomes = async (id) => {
  const data = await axios.get(`${API_URL}/${id}`);
  return data.data;
};

const fetchData = async () => {
  setLoading(true);

  const res = await axios.get("${API_URL}");

  // Create array of promises
  const promises = res.data.map(el => {
    return fetchIncomes(el.id)
      .then(fromApi => {
        return {
          ...el,
          ...fromApi
        };
      });
  });

  // Await all promises to resolve
  const newData = await Promise.all(promises);

  // Update state with new data
  setData(newData);
  setLoading(false);
};
注意:如果您愿意,还可以使用async Wait作为承诺数组:

const promises = res.data.map(async (el) => {
  const fromApi = await fetchIncomes(el.id);
  return {
    ...el,
    ...fromApi
  };
});

您需要等待
返回的承诺

试试这个:

const fetchIncomes = async (id) => {
  const data = await axios.get(`${API_URL}/${id}`);
  return data.data;
};

const fetchData = async () => {
  setLoading(true);

  const res = await axios.get("${API_URL}");

  // Create array of promises
  const promises = res.data.map(el => {
    return fetchIncomes(el.id)
      .then(fromApi => {
        return {
          ...el,
          ...fromApi
        };
      });
  });

  // Await all promises to resolve
  const newData = await Promise.all(promises);

  // Update state with new data
  setData(newData);
  setLoading(false);
};
注意:如果您愿意,还可以使用async Wait作为承诺数组:

const promises = res.data.map(async (el) => {
  const fromApi = await fetchIncomes(el.id);
  return {
    ...el,
    ...fromApi
  };
});
您好:)我还有一个问题,因为现在页面仅在刷新后加载,我如何解决它?您好:)我还有一个问题,因为现在页面仅在刷新后加载,我如何解决它?