【问题标题】:data extraction with javascript fetch api使用 javascript fetch api 提取数据
【发布时间】:2021-11-28 11:07:04
【问题描述】:

我正在使用 fetch api 提取数据。但我无法检索我提取的最后一个数据的 todosApi 部分中的数据。如何提取数据?

const usersApi = () =>{
    fetch("https://jsonplaceholder.typicode.com/users").
    then(response=>response.json()).
    then(girilenVeri).
    catch(e=>console.log(e));
}

const todosApi = (element) =>{
    fetch(`https://jsonplaceholder.typicode.com/todos/?userId=${element.id}`).
    then(response=>veriOlusturucu(response.json(), element)).//I can't get the data in response.json
    catch(e=>console.log(e));
}

const girilenVeri = (data) => {
    let cumle = [];
    document.getElementById('arama').addEventListener('keydown',function(e){
        if(e.keyCode == 8){
            cumle.pop();  
            veriEslestir(data, cumle);
        }
    });
    document.getElementById('arama').addEventListener('keypress',function(e){
        cumle.push(String.fromCharCode(e.keyCode));
        veriEslestir(data, cumle);
    });
}

const veriEslestir = (data,cumle) =>{
    veri = cumle.toString().replace(/,/g,"");
    data.forEach(element => {
        if(element.username.toLowerCase() == veri.toLowerCase()){
            todosApi(element);
        }
    });
}

const veriOlusturucu = (todo,element) => {
    console.log(todo);
    console.log(element);
    let html = "";
    html =`
                <h5 class="card-title">İletişim</h5>
                <ul class="list-group">
                    <li class="list-group-item">Kullanıcı Adı: ${element.username}</li>
                    <li class="list-group-item">E-Mail: ${element.email}</li>
                    <li class="list-group-item">Web Site: ${element.website}</li>
                    <li class="list-group-item">Şirket: ${element.company.name}</li>
                    <li class="list-group-item">Telefon No: ${element.phone}</li>
                    <li class="list-group-item">Adres: ${element.address.street} ${element.address.suite} ${element.address.city} ${element.address.zipcode}</li>
                </ul>
                <h5 class="card-title">Yapılacaklar Listesi</h5>
                <ul class="list-group">
    `;

    todo.forEach(element=>{//I need to access the data here with loop
        html+=`
            <li class="list-group-item">Kullanıcı Adı: ${element.title}</li>
        `;
    });

    html +=`</ul>`;
    document.getElementById('veriListele').innerHTML=html;
}

document.addEventListener('DOMContentLoaded',usersApi());

如何使用 foreach 返回“response.json”部分? 用户信息没有问题。但是todo信息有问题。把它作为一个承诺寄给我。我无法访问承诺结果

如果我能进入“PromiseResult”,问题就会得到解决。但我无法到达

【问题讨论】:

  • 您缺少对用户执行的步骤 - “then(response=>response.json())”
  • @maria,可能存在比让response.json() 工作更大的问题。由于todosApi 是异步的,因此无法保证连续的todosApi() 调用将以正确的顺序完成。这会一直影响到“keydown”/“keypress”处理程序;无法保证对连续事件的响应顺序正确。

标签: javascript promise fetch-api


【解决方案1】:

您没有完全正确地使用 fetch api 与待办事项列表。如果您注意到,在您的 userApi 方法中,您包含一个额外的 .then ,这是返回 json 数据而不是 promise 所必需的:

const usersApi = () =>{
  fetch("https://jsonplaceholder.typicode.com/users").
  then(response=>response.json()).
  then(girilenVeri).
  catch(e=>console.log(e));
}

 const todosApi = (element) =>{
   fetch(`https://jsonplaceholder.typicode.com/todos/?userId=${element.id}`)
   .then(response=>response.json())
   .then(data => veriOlusturucu(data, element))
   catch(e=>console.log(e));
}

试试这个。

【讨论】:

  • .then(json。那时它不再是 JSON。
  • @silencedogood 谢谢。你太棒了。
  • response 是 JSON,但是你用 response.json() 解析它。所以下一步的信息是一个JS结构(数组/对象等),而不是JSON。没有 JSON 对象之类的东西。
  • @Andy Ahh。适当的语义。戈奇亚。谢谢指正!
  • 是的,就叫它then(data =&gt; ...吧。
猜你喜欢
  • 2022-01-11
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
  • 2019-01-05
相关资源
最近更新 更多