Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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
Api 如何从导出fetch函数中获取作为数组的数据?_Api_Webpack_Fetch - Fatal编程技术网

Api 如何从导出fetch函数中获取作为数组的数据?

Api 如何从导出fetch函数中获取作为数组的数据?,api,webpack,fetch,Api,Webpack,Fetch,我正在使用webpack制作一个电影列表web应用程序。当我导出用于获取数据并将其导入index.js的函数时,控制台中有一个空数组。为什么我不能从导出fetch函数中获取return值?我还检查了navData的数据长度,然后它显示为0。有人能帮我解决这个问题吗?谢谢 api.js export function fetchTopgames(url) { let dataArr = [] fetch(url, { method: 'GET', headers: {

我正在使用webpack制作一个电影列表web应用程序。当我导出用于获取数据并将其导入index.js的函数时,控制台中有一个空数组。为什么我不能从导出fetch函数中获取
return
值?我还检查了navData的数据长度,然后它显示为0。有人能帮我解决这个问题吗?谢谢

api.js

export function fetchTopgames(url) {
  let dataArr = []
  fetch(url, {
    method: 'GET',
    headers: {
      'Client-ID': clientID,
      'Accept': 'application/vnd.twitchtv.v5+json'
    }
  }).then((resp) => {
    if (resp.status >= 200 && resp.status < 300) {
      return resp.json();
    }
  }).then((data) => {
    dataArr.push(data.top)
  }).catch((error) => {
    console.log(error.response.json());
  });
  return dataArr
};
[![import { fetchTopgames } from './api';
const navData = fetchTopgames(topGameAPI)
console.log(navData)][1]][1]