Javascript 如何使用array.map方法获取数组中每个元素的数据
我想为数组中的每个对象获取数据,并返回一个包含以前和新获取的数据的新对象数组。由于我的函数返回一个包含已解析的未定义承诺的数组,因此我无法获取结果数组。 我正在使用一个航班搜索api,该api使用apca的函数进行抓取Javascript 如何使用array.map方法获取数组中每个元素的数据,javascript,arrays,node.js,reactjs,async-await,Javascript,Arrays,Node.js,Reactjs,Async Await,我想为数组中的每个对象获取数据,并返回一个包含以前和新获取的数据的新对象数组。由于我的函数返回一个包含已解析的未定义承诺的数组,因此我无法获取结果数组。 我正在使用一个航班搜索api,该api使用apca的函数进行抓取 export const searchApcaLocation = async (dataArr,setDeals) => { const promises = await dataArr.map(async item => { apca.re
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
}
有人能告诉我我做错了什么吗
编辑:
当我试图修复它时,我意识到问题是我没有返回map函数中的任何内容,但是如果试图返回apca.onSuccess,我将得到一个函数数组只是
返回
在fetch函数之前丢失。因为你没有返回你的承诺结果,它给出了未定义的结果
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
}
在fetch函数之前,缺少just
return
。因为你没有返回你的承诺结果,它给出了未定义的结果
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
}
在您的案例中,问题可能是,您正在使用async/await,然后一起使用块。 让我总结一下正在发生的事情: 1) 您正在等待dataArray.map 2) 在map回调中,使用apca的onSuccess方法 3) 在这个方法中,您使用的是then块,它不会等待您得到响应 在返回locationObject时,函数已经到达return语句并尝试返回结果。 但结果当然是未定义的,因为它们根本无法得到解决 另外,请记住,您的函数返回另一个承诺,因为您使用了async/await,您必须在导入它的位置解析它
干杯:)在您的情况下,问题可能是,您正在使用async/await,然后一起阻塞。 让我总结一下正在发生的事情: 1) 您正在等待dataArray.map 2) 在map回调中,使用apca的onSuccess方法 3) 在这个方法中,您使用的是then块,它不会等待您得到响应 在返回locationObject时,函数已经到达return语句并尝试返回结果。 但结果当然是未定义的,因为它们根本无法得到解决 另外,请记住,您的函数返回另一个承诺,因为您使用了async/await,您必须在导入它的位置解析它
干杯:)刚试过,但我得到的是一个未定义值的数组,而不是对象。在控制台记录这些步骤之后,我发现我的results const没有等待promises const获取data@alexamirov可以辨别什么是apca并添加虚拟的
dataArr
。我将共享正确的代码块。我刚刚尝试过,但我得到的是一个具有未定义值的数组,而不是对象。在控制台记录这些步骤之后,我发现我的results const没有等待promises const获取data@alexamirov可以辨别什么是apca并添加虚拟的dataArr
。我将共享适当的代码块嘿,刚刚将我的所有THEN移到了async/await,但遇到了相同的问题,即我的函数返回未定义,因为它无法解析结果可能apca对象不是设计为处理async await的。您是否在返回.log(locationObject)之前尝试过将其控制台化?只是为了确保您得到响应。嘿,刚刚将我的所有THEN移到了async/await,但遇到了相同的问题,即我的函数返回未定义,因为它没有解析结果。可能apca对象不是设计为处理async await的。您是否在返回.log(locationObject)之前尝试过将其控制台化?只是为了确保你得到回应。