首页 > 解决方案 > 嵌套异步 axios 调用只返回一个对象

问题描述

已经坚持这个一个多小时了,所以有人能指出我正确的方向吗?

在下面的代码片段中,我只取回一个对象而不是多个对象。

我用我的代码片段之间的注释非常详细地解释了这个问题,所以问题应该很清楚。

  useEffect(() => {
    axios.get('url-here').then((res) => {
      res.data.favProperties?.map((el) => {
        console.log(el) // this returns id's of saved/liked items
        axios.get('url-here').then(async (r) => {
          if (r.data) {
            console.log(r.data) // Problem starts here
            // This returns the full object of the liked items
            // But only one object is returned, not every object for which an id was stored
            await storageRef
              .child(r.data.firebaseRef + '/' + r.data.images[0])
              .getDownloadURL()
              .then((url) => {
                // Here i need to fetch the image for each object
                console.log(url)
              })
              .catch((err) => console.log(err))
          }
        })
      })
    })
  }, [])

感谢您调查它!亲切的问候

标签: reactjsasync-awaitaxios

解决方案


推荐阅读