【问题标题】:Fetch with Axios and React-Redux使用 Axios 和 React-Redux 获取
【发布时间】:2018-11-14 00:36:36
【问题描述】:

我在我的 react-redux 应用程序中使用 axios。这是我的 API 调用:

让数组 = []; 让数组2 = [1,2,3];

            for (let i = 0; i < array2.length; i++) {
                array.push(axios.get(`https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=${array2[i]}`))

            }

            const array3 = axios.all(array).then(response);

如果我在 then() 中执行 response 的 console.log,我会得到想要的结果 (Array[5]),但是如果我执行 array3,输出为:

承诺 原型:承诺 [[PromiseStatus]]:“已解决” [[PromiseValue]] : 数组[5]

如何提取包含在 [[PromiseValue]] 中的数组的信息并将它们放入另一个数组中?到目前为止,我只有使用 React 的经验,但我不知道如何使用 react-redux 处理并行获取的数据。 谢谢你的期待!

【问题讨论】:

  • axios 返回一个承诺,这就是你得到的。您需要使用 .then().catch() 处理该承诺
  • 谢谢!太感谢了!我找到了解决方案,我将在下面的评论中写下。

标签: react-redux axios


【解决方案1】:

解决方案: 删除最后一行代码( const array3 = axios.all(array).then(response); )并写:

const array3 = [ ]

axios.all(array).then(response => array3.push(response.map((d, index) => d.data)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多