简体   繁体   English

如何使用 array.map 方法获取数组中每个元素的数据

[英]How to fetch data on every element in an array using array.map method

I want to fetch data for every object in an array and return an array of new objects with the previous and newly fetched data.I got stucked on getting my result array as my function is returning an array of resolved undefined promises.我想为数组中的每个 object 获取数据,并返回一个包含先前和新获取的数据的新对象数组。由于我的 function 正在返回一组已解决的未定义承诺,因此我无法获取结果数组。 I am using a flight search api thats using the apca function for fetching我正在使用航班搜索 api 那就是使用apca function 来获取

export const searchApcaLocation = async (dataArr,setDeals) => {
    const promises = await dataArr.map(async item => {
        apca.request(item.destination);
        apca.onSuccess = (data) => {
            return fetch('http://localhost:3050/googlePlaceSearch',{
                        method:"post",
                        headers:{'Content-Type':'application/json'},
                        body:JSON.stringify({
                            cityName:data.airports[0].city
                            })
            })
            .then(res => res.json())
            .then(imagelinkData => {
                const locationObject = {
                    data: item,
                    imagelink: imagelinkData.link
                }
                return locationObject
            })
            .catch(err => console.log('error on image search',err))
        };
        apca.onError = (data) => {
            console.log('error',data)
        };
    })
    const results = await Promise.all(promises)
    return results
}

can someone guide me please on what am I doing wrong?有人可以指导我我做错了什么吗?

edit: as I am trying to fix it realized the problem is I am not returning anything in my map function but if trying to return the apca.onSuccess I am getting an array of functions编辑:当我试图修复它时,我意识到问题是我没有在我的 map function 中返回任何内容,但是如果尝试返回 apca.onSuccess 我得到了一系列函数

just return is missing before fetch function.在获取 function 之前缺少return since you're not returning your promise result it's giving undefined.因为您没有返回 promise 结果,所以它给出了未定义的结果。

export const searchApcaLocation = async (dataArr,setDeals) => {
    const promises = await dataArr.map(async item => {
        apca.request(item.destination);
        apca.onSuccess = (data) => {
            return fetch('http://localhost:3050/googlePlaceSearch',{
                        method:"post",
                        headers:{'Content-Type':'application/json'},
                        body:JSON.stringify({
                            cityName:data.airports[0].city
                            })
            })
            .then(res => res.json())
            .then(imagelinkData => {
                const locationObject = {
                    data: item,
                    imagelink: imagelinkData.link
                }
                return locationObject
            })
            .catch(err => console.log('error on image search',err))
        };
        apca.onError = (data) => {
            console.log('error',data)
        };
    })
    const results = await Promise.all(promises)
    return results
}

The issue in your case might be, that you are using async/await and then blocks together.您的问题可能是,您正在使用 async/await 然后一起阻塞。 Let me sum up what is happening:让我总结一下正在发生的事情:

1) you await dataArray.map 1)你等待 dataArray.map

2) within the map callback, you use the onSuccess method of apca 2)在 map 回调中,使用 apca 的 onSuccess 方法

3) within this method you are using then blocks which won't await until you got a response. 3)在此方法中,您正在使用 then 块,这些块在您得到响应之前不会等待。

At this point where you return the locationObject, your function already reached the return statement and tries to return results.此时您返回 locationObject,您的 function 已经到达 return 语句并尝试返回结果。 But results are of course undefined because they never get resolved at all.但是结果当然是不确定的,因为它们根本没有得到解决。

Also, keep in mind that your function returns another promise because you used async/await which you have to resolve where you imported it.另外,请记住,您的 function 返回另一个 promise 因为您使用了 async/await ,您必须解决导入它的位置。

Cheers:)干杯:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM