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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 fetch 中檢索對(duì)象屬性數(shù)組,然后將其傳遞給第二個(gè) fetch 調(diào)用?

如何在 fetch 中檢索對(duì)象屬性數(shù)組,然后將其傳遞給第二個(gè) fetch 調(diào)用?

浮云間 2021-10-29 15:04:10
我有一個(gè)依賴于 2 個(gè)端點(diǎn)來檢索所需程序名稱的組件。我有 2 個(gè)端點(diǎn)。第一個(gè)端點(diǎn)返回程序列表,它是一個(gè)對(duì)象數(shù)組。目前,它只返回 4 個(gè)程序(2 個(gè)程序 ID 為“13”,另外兩個(gè)程序 ID 為“14”)。第二個(gè)端點(diǎn)依賴于這些程序 id,我需要以某種方式將它傳遞給第二個(gè)端點(diǎn),避免重復(fù),并用逗號(hào)分隔。例如:首先獲取調(diào)用響應(yīng):{  "programs": [    { "id": 1, "organization": {"organizationId": 2000}, "programId": 13 },    { "id": 2, "organization": {"organizationId": 2001 }, "programId": 13 },    { "id": 22, "organization": {"organizationId": 2002 }, "programId": 14 },    { "id": 20, "organization": {"organizationId": 2000 }, "programId": 14 }  ]}第二次獲取調(diào)用:api/v1/program/programlist/13,14在代碼中,這是我到目前為止所做的:fetch('/api/v1/organizationrelationship/organizationprograms', {  method: 'GET',  headers: {    'Content-Type': 'application/json',  }}).then(res => res.json()).then(res => {  if(res) {    _.map(res.programs, (program, index) => {      fetch(`/api/v1/program/programlist/${program.programId}`, { // this is passing all 4 programId individually        method: 'GET',        headers: {          'Content-Type': 'application/json',        }      })      .then(res => res.json())      .then(res => {        if(res) {          console.log("res: ", res)        }      })    })  }})我正在尋找的所需工作流程:第一次 fetch 調(diào)用成功然后轉(zhuǎn)到第二個(gè) fetch 調(diào)用并在 url 中傳入 programIdsapi/v1/program/programlist/13,14然后我將響應(yīng)保存在組件的狀態(tài)中
查看完整描述

1 回答

?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊

試試下面的代碼片段,它應(yīng)該可以解決問題。


fetch('/api/v1/organizationrelationship/organizationprograms', {

  method: 'GET',

  headers: {

    'Content-Type': 'application/json',

  }

})

.then(res => res.json())

.then(res => {

  if(res) {

    // gets all the ids from the response and make them a set to remove duplicate

    let ids = new Set(res.proprams.map( (program, index) => return program.programId));

    // convert the set into and array and the use the toString function to make them comma seperated

    let params = Array.from(ids).toString()

      fetch(`/api/v1/program/programlist/${params}`, { // this is passing all 4 programId individually

        method: 'GET',

        headers: {

          'Content-Type': 'application/json',

        }

      })

      .then(res => res.json())

      .then(res => {

        if(res) {

          //here you can now save the response to state

          console.log("res: ", res)

        }

      })

  }


})


查看完整回答
反對(duì) 回復(fù) 2021-10-29
  • 1 回答
  • 0 關(guān)注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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