【问题标题】:Promise.all Showing data in loop console but not passing data to statePromise.all 在循环控制台中显示数据但不将数据传递给状态
【发布时间】:2020-11-20 12:51:11
【问题描述】:

我对多个 API 调用的 Promise.all 函数有问题。我正在进行多个 api 调用,然后 console.log 结果。在 map 函数中执行 console.log 时,我能够看到结果,但是当尝试设置状态以在 UI 中呈现和显示时,数据不会流向状态。我不知道问题到底出在哪里以及为什么数据没有流向状态。

这是我的代码。

    componentDidMount() {
        
        const promises = data.map(id => {
            return new Promise((resolve) => {
              fetch(`https://example.com/api/ITems/${id.ID}`)
                .then(response => {
                  return new Promise(() => {
                    response.json()
                    .then(response => {
                        console.log(response); // this console log is showing me data into console, but the console.log below in promise.all section is not showing any data
                        resolve()
                      })
                      .catch((error) => {
                            console.log(error);
                        })
                  })
                })
            })
          })

        Promise.all(promises).then(results => {
            this.setState({Ids: results.data}); // no data is passing to state
           console.log(results); // this console.log does not work. when remove the console.log in loop, i cannot see anything. data is not flowing here
        })
        .catch((error) => {
            console.log(error);
        })
      }
    

谢谢大家。

【问题讨论】:

  • console.log(error);s 中的任何一个都在记录什么吗?
  • 嗨@CertainPerformance。控制台没有错误。
  • Promise.all(promises).then(results => { this.setState({Ids: results.data}); console.log(results); }) 数据不在这里

标签: reactjs jsx


【解决方案1】:

发生这种情况是因为您将其解析为未定义,因此您的所有承诺返回值都是 undefined

将其更改为:resolve(response),这样它实际上会使用您想要的 response 解决

    const promises = data.map(id => {
        return new Promise((resolve) => {
          fetch(`https://example.com/api/ITems/${id.ID}?realm=xyz`, { headers })
            .then(response => {
              return new Promise(() => {
                return response.json() // <--------------------------- Additionally you need to return this as well
                .then(response => {
                    console.log(response); // this console log is showing me data into console, but the console.log below in promise.all section is not showing any data
                    resolve(response) // <---------------------------- ISSUE is here
                  })
                  .catch((error) => {
                        console.log(error);
                    })
              })
            })
        })
      })

【讨论】:

  • 嗨@Rikin。你说的对。 promise.all 部分中的数组在控制台中未定义。现在数据来了。该控制台正在工作。但仍有挑战。数据仍未处于状态。 Promise.all(promises) .then(response => { this.setState({Ids: response.data}); // 仍然没有数据 console.log(response); // 这是有效的。在控制台中显示数据 })
  • 构造函数(props) { super(props); this.state = { ID:[] }; console.log(this.state.Ids); // 这里没有数据 }
  • @Raza 我想您只需要查看 console.log 中对象的形状并进行相应调整。可能是它的response.data.data?如果没有它如何登录控制台的形状,我无法判断。如果您不确定,请尝试console.log(JSON.stringify(response.data)) 并告诉我,我会为您指出。
  • 非常感谢@Rikin。它现在工作。最后一个问题是由于this.setState({Ids: response.data}); 我将.data 与响应连接起来。非常感谢,非常感谢
【解决方案2】:

你不是resolveing 任何东西。考虑只在Promise.all 中避免explicit promise construction antipattern.catch

const promises = data.map(id => 
  fetch(`https://example.com/api/ITems/${id.ID}?realm=xyz`, { headers })
    .then(res => res.json())
    .then(result => result.data)
 );
Promise.all(promises).then(results => {
  this.setState({Ids: results});
})
.catch((error) => {
  console.log(error);
});

解析为Promise.all 的值将始终是一个数组 - 它没有.data 属性。听起来每个单独的fetch 结果都返回一个具有data 属性的对象,因此您应该像上面那样在.map 中执行此操作,而不是在Promise.all 中。

【讨论】:

  • 非常感谢@CertainPerformance。非常感谢。这真的很有帮助。
猜你喜欢
  • 2021-10-23
  • 1970-01-01
  • 2019-08-19
  • 2023-02-13
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 2021-11-29
相关资源
最近更新 更多