I've been using Vue and attempting to make some axios requests within a map method over an array, but I'm getting unexpected results.
Here's my function:
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)
}
}
I was expecting to receive an array of 6 JSON objects, but instead, I'm getting an array of 6 arrays with the same 6 JSON objects inside each one. Can anyone shed some light on why this is happening?