Asynchronous 推送后,Axios阵列的响应返回未定义

Asynchronous 推送后,Axios阵列的响应返回未定义,asynchronous,axios,Asynchronous,Axios,我正在尝试使用Axios获取前十个口袋妖怪,然后将对象的数据传递到数组中。在那之后,我想按重量过滤并返回口袋妖怪,它们的重量小于参数 但推入数组返回未定义的结果。我不知道为什么?请帮忙 我的代码: findLessWeight (weight) { let firstTenPokemons = []; for (let i = 1; i < 11; i++) { let url = `https://pokeapi.co/api/v2/pokemon/${i}`;

我正在尝试使用Axios获取前十个口袋妖怪,然后将对象的数据传递到数组中。在那之后,我想按重量过滤并返回口袋妖怪,它们的重量小于参数

但推入数组返回未定义的结果。我不知道为什么?请帮忙

我的代码:

findLessWeight (weight) {
  let firstTenPokemons = [];
  for (let i = 1; i < 11; i++) {
    let url = `https://pokeapi.co/api/v2/pokemon/${i}`;
    axios.get(url)
    .then(response=> {
      firstTenPokemons.push(response.data);
    })   
  }
  return firstTenPokemons.filter(pokemon=>pokemon.weight < weight);
}  
findLessWeight(重量){
让firstTenPokemons=[];
for(设i=1;i<11;i++){
让url=`https://pokeapi.co/api/v2/pokemon/${i}`;
获取(url)
。然后(响应=>{
firstTenPokemons.push(response.data);
})   
}
返回firstTenPokemons.filter(pokemon=>pokemon.weight
您必须让for循环等待,直到axios调用完成

async findLessWeight (weight) {
  let firstTenPokemons = [];
  for (let i = 1; i < 11; i++) {
    let url = `https://pokeapi.co/api/v2/pokemon/${i}`;
    let response = await axios.get(url)
    firstTenPokemons.push(response.data);
  }
  return firstTenPokemons.filter(pokemon=>pokemon.weight < weight);
}
async findLessWeight(权重){
让firstTenPokemons=[];
for(设i=1;i<11;i++){
让url=`https://pokeapi.co/api/v2/pokemon/${i}`;
let response=等待axios.get(url)
firstTenPokemons.push(response.data);
}
返回firstTenPokemons.filter(pokemon=>pokemon.weight
您必须让for循环等待,直到axios调用完成

async findLessWeight (weight) {
  let firstTenPokemons = [];
  for (let i = 1; i < 11; i++) {
    let url = `https://pokeapi.co/api/v2/pokemon/${i}`;
    let response = await axios.get(url)
    firstTenPokemons.push(response.data);
  }
  return firstTenPokemons.filter(pokemon=>pokemon.weight < weight);
}
async findLessWeight(权重){
让firstTenPokemons=[];
for(设i=1;i<11;i++){
让url=`https://pokeapi.co/api/v2/pokemon/${i}`;
let response=等待axios.get(url)
firstTenPokemons.push(response.data);
}
返回firstTenPokemons.filter(pokemon=>pokemon.weight
Axios查询本质上是AJAX调用的包装器,异步运行。因此,函数的
return
语句没有等待查询返回,这意味着函数总是返回一个空数组。您可以使用等待异步查询运行,以及使用
axios.all()
并行发送请求(而不必逐个等待)

在下面的代码片段中,我们使用
async
关键字声明您的
findLessWeight
函数,将请求构建在一个数组中,等待它们同时使用
axios执行。所有
等待
,然后最终返回过滤结果。我还添加了另一个函数
main
,展示如何使用
findLessWeight
。它是另一个异步函数,等待
findLessWeight
完成执行,然后记录结果

注意:函数只记录权重,因为响应对象很大

异步函数findLessWeight(w){
//承诺(请求)数组
常量firstTenPokemonPromises=[];
for(设i=1;i<11;i++){
//一个接一个地增加承诺
firstTenPokemonPromises.push(
axios.get(`https://pokeapi.co/api/v2/pokemon/${i}`)
//我们只需要回复中的数据
。然后({data})=>(data))
);
}
//等待它们并行执行
const firstTenPokemons=等待axios.all(firstTenPokemonPromises);
//返回筛选结果
返回firstTenPokemons.filter(({weight})=>(weight(weight));
}
main()

Axios查询本质上是AJAX调用的包装,它异步运行。因此,函数的
return
语句没有等待查询返回,这意味着函数总是返回一个空数组。您可以使用等待异步查询运行,以及使用
axios.all()
并行发送请求(而不必逐个等待)

在下面的代码片段中,我们使用
async
关键字声明您的
findLessWeight
函数,将请求构建在一个数组中,等待它们同时使用
axios执行。所有
等待
,然后最终返回过滤结果。我还添加了另一个函数
main
,展示如何使用
findLessWeight
。它是另一个异步函数,等待
findLessWeight
完成执行,然后记录结果

注意:函数只记录权重,因为响应对象很大

异步函数findLessWeight(w){
//承诺(请求)数组
常量firstTenPokemonPromises=[];
for(设i=1;i<11;i++){
//一个接一个地增加承诺
firstTenPokemonPromises.push(
axios.get(`https://pokeapi.co/api/v2/pokemon/${i}`)
//我们只需要回复中的数据
。然后({data})=>(data))
);
}
//等待它们并行执行
const firstTenPokemons=等待axios.all(firstTenPokemonPromises);
//返回筛选结果
返回firstTenPokemons.filter(({weight})=>(weight(weight));
}
main()