我正在使用 Vue,我正在尝试在 map over array 中发出一些 axios 请求,但我得到了非常糟糕的结果。
我的功能是:
async getFavoriteRecipes(){
try{
let myRecipes=[]
const response = await this.axios.get("https://david-matan-recipe-api-server.herokuapp.com/api/profiles/myprofile")
let myFavoritedIds=response.data.favoriteRecipe;
myRecipes= await Promise.all(myFavoritedIds.map(async (recipeInfo) =>{
if(id.type==="spooncalur"){
const result = await this.axios.get("https://david-matan-recipe-api-server.herokuapp.com/api/recipes/"+recipeInfo.id)
myRecipes.push(result.data)
return myRecipes
}
else{
const result = (await this.axios.get("https://david-matan-recipe-api-server.herokuapp.com/api/recipes/userecipe/"+recipeInfo.id))
myRecipes.push(result.data)
return myRecipes
}
}
))
this.myFavoriteRecipe=myRecipes
}
catch(err)
{
if(err.response.status==='401'){
this.$root.store.username=undefined
this.$router.push('/login')
}
console.log(err.response)
}
}
我期望得到 6 个 json 对象的数组,但我得到了一个 6 个数组的数组,每个数组都包含相同的 6 个 json 对象。有人可以解释我为什么会这样吗?
Smart猫小萌
交互式爱情
相关分类