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

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

Javascript 在地圖內(nèi)獲取

Javascript 在地圖內(nèi)獲取

九州編程 2023-06-29 22:31:30
我有一個(gè)網(wǎng)站/作品集,我使用 Github API 顯示我的所有項(xiàng)目。我的目標(biāo)是為這些項(xiàng)目創(chuàng)建一個(gè)過濾器,因此我在一些存儲(chǔ)庫的根目錄中創(chuàng)建了一個(gè)名為“built-with.json”的文件,該文件僅存在于兩個(gè)存儲(chǔ)庫中,僅用于測(cè)試目的,這是我的一系列技術(shù)在項(xiàng)目中使用(例如:[“React”,“Javascript”,...])。所以我需要獲取 Github APi(該部分運(yùn)行良好),然后獲取該文件,并返回一個(gè)新的項(xiàng)目數(shù)組,但帶有“filters”鍵,其中值是“built-with.json”內(nèi)的數(shù)組。例子:Github API返回(僅一個(gè)項(xiàng)目返回的示例):[{"id": 307774617,"node_id": "MDEwOlJlcG9zaXRvcnkzMDc3NzQ2MTc=","name": "vanilla-javascript-utility-functions","full_name": "RodrigoWebDev/vanilla-javascript-utility-functions","private": false}]我需要的新對(duì)象數(shù)組:[{"id": 307774617,"node_id": "MDEwOlJlcG9zaXRvcnkzMDc3NzQ2MTc=","name": "vanilla-javascript-utility-functions","full_name": "RodrigoWebDev/vanilla-javascript-utility-functions","private": false,"filters": ["HTML5", "CSS3", "JS", "React"]}]這就是我所做的:const url = "https://api.github.com/users/RodrigoWebDev/repos?per_page=100&sort=created";fetch(url)  .then((response) => response.json())  .then((data) => {      return data.map(item => {        //item.full_name returns the repositorie name        fetch(`https://raw.githubusercontent.com/${item.full_name}/master/built-with.json`)          .then(data => {            item["filters"] = data            return item          })      })    })  .then(data => console.log(data))但這不起作用!我在控制臺(tái)中得到這個(gè):有人可以幫助我嗎?提前致謝
查看完整描述

3 回答

?
一只萌萌小番薯

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

這里有幾件事。您不需要將 .then() 鏈接到 fetch() 上。fetch() 返回一個(gè)承諾。Array.prototype.map() 返回一個(gè)數(shù)組??偠灾阕罱K會(huì)得到一系列的承諾。您可以使用 Promise.all(arrayOfPs) 解析 Promise 數(shù)組


編輯:在您發(fā)表評(píng)論并審查您的問題后,我重寫了此內(nèi)容,以便它從過濾后的存儲(chǔ)庫列表中檢索技能。


const url = `https://api.github.com/users/RodrigoWebDev/repos?per_page=100&sort=created`;


(async() => {

  // Final results 

  let results;

  try {

    // Get all repositories

    const repos = await fetch(url).then((res) => res.json());

    const responses = await Promise.all(

      // Request file named 'build-with.json' from each repository

      repos.map((item) => {

        return fetch(

          `https://raw.githubusercontent.com/${item.full_name}/master/built-with.json`

        );

      })

    );

    // Filter out all non-200 http response codes (essentially 404 errors)

    const filteredResponses = responses.filter((res) => res.status === 200);

    results = Promise.all(

      // Get the project name from the URL and skills from the file

      filteredResponses.map(async(fr) => {

        const project = fr.url.match(/(RodrigoWebDev)\/(\S+)(?=\/master)/)[2];

        const skills = await fr.json();

        return {

          project: project,

          skills: skills

        };

      })

    );

  } catch (err) {

    console.log("Error: ", err);

  }

  results.then((s) => console.log(s));

})();


查看完整回答
反對(duì) 回復(fù) 2023-06-29
?
largeQ

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

問題是提取沒有被返回,因此 .map() 返回未定義。我可以建議使用 async-await 的解決方案嗎?


const url = "https://api.github.com/users/RodrigoWebDev/repos?per_page=100&sort=created";


getData(url).then(data => console.log(data));

  

async function getData(url){

  const response = await fetch(url);

  const data = await response.json();

  const arrOfPromises = data.map(item => fetch(`https://raw.githubusercontent.com/${item.full_name}/master/built-with.json`)

  );

  return Promise.all(arrOfPromises);

}


查看完整回答
反對(duì) 回復(fù) 2023-06-29
?
人到中年有點(diǎn)甜

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

您有多個(gè)問題:

  1. 在地圖函數(shù)內(nèi)部,您不返回任何結(jié)果

  2. 地圖函數(shù)的結(jié)果實(shí)際上是另一個(gè) Promise(因?yàn)閮?nèi)部有 fetch)。

那么你需要做什么:

  1. 從地圖返回承諾 - 因此你將擁有一系列承諾

  2. 使用 Promise.all 等待第 1 點(diǎn)中的所有承諾

像這樣的東西:


 var url1 = "https://api.github.com/users/RodrigoWebDev/repos?per_page=100&sort=created";

    var datum = fetch(url1)

      .then((response) => response.json())

      .then((data) => {

          return Promise.all(data.map(item => {

            //item.full_name returns the repositorie name

            return fetch(`https://raw.githubusercontent.com/${item.full_name}/master/built-with.json`)

              .then(data => {

                item["filters"] = data

                return item

              })

          }));

        }).then(data => console.log(data))


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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