第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Promise.all 在數(shù)組映射中的不良行為?

Promise.all 在數(shù)組映射中的不良行為?

哆啦的時光機 2022-10-21 09:34:14
我正在使用 Vue,我正在嘗試在 map over array 中發(fā)出一些 axios 請求,但我得到了非常糟糕的結(jié)果。我的功能是:  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 對象的數(shù)組,但我得到了一個 6 個數(shù)組的數(shù)組,每個數(shù)組都包含相同的 6 個 json 對象。有人可以解釋我為什么會這樣嗎?
查看完整描述

2 回答

?
Smart貓小萌

TA貢獻1911條經(jīng)驗 獲得超7個贊

看起來,您想要一個包含每個請求的結(jié)果數(shù)據(jù)的數(shù)組。所以我建議不要將數(shù)據(jù)推送到myRecipes數(shù)據(jù)以返回它。這將自動在列表中“添加”(或更好地替換)它。代碼將如下所示:


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)

                return result.data

            } else {

                const result = (await this.axios.get("https://david-matan-recipe-api-server.herokuapp.com/api/recipes/userecipe/" + recipeInfo.id))

                return result.data

            }

        }))

        this.myFavoriteRecipe = myRecipes

    } catch (err) {

        if (err.response.status === '401') {

            this.$root.store.username = undefined

            this.$router.push('/login')

        }

        console.log(err.response)

    }

}

如果您不了解該map功能,這可能會有所幫助https://www.youtube.com/watch?v=DC471a9qrU4或https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array /地圖


查看完整回答
反對 回復(fù) 2022-10-21
?
交互式愛情

TA貢獻1712條經(jīng)驗 獲得超3個贊

Ayk 已經(jīng)解釋了問題的原因......但代碼也可以簡化為


const path = "https://david-matan-recipe-api-server.herokuapp.com/api/recipes/" + 

  id.type==="spooncalur" ? '' : 'userecipe/'

  

myRecipes = await Promise.all(myFavoritedIds.map(recipeInfo =>

  this.axios.get(path + recipeInfo.id)

))

無需進行this.axios.get(path + recipeInfo.id)異步,因為 Promise 都將一組 Promise 作為參數(shù),而不是結(jié)果值


查看完整回答
反對 回復(fù) 2022-10-21
  • 2 回答
  • 0 關(guān)注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號