TypeError:data.forEach不是javascript中的函数,用于获取数据表单api

时间:2019-05-07 10:18:23

标签: javascript ecmascript-6

enter image description here

document.querySelector("#userForm").addEventListener('click', getProfile);
function getProfile(event) {
    event.preventDefault();
    var username = document.querySelector('#searchUser').value;
    fetch(`https://api.github.com/users/${username}`).then(function (res) {
      return res.json();
    }).then(function (data) {
let output = '';
data.forEach(function (user) {
        output +=`
        <div class="card card-body mb-3">
        <div class="row">
          <div class="col-md-3">
            <img class="img-fluid mb-2" src="${user.avatar_url}">
            <a href="${user.html_url}" target="_blank" class="btn btn-primary btn-block mb-4">View Profile</a>
          </div>
          <div class="col-md-9">
            <span class="badge badge-primary">Public Repositories: ${user.public_repos}</span>
            <span class="badge badge-secondary">Public Gists: ${user.public_gists}</span>
            <span class="badge badge-success">Followers: ${user.followers}</span>
            <span class="badge badge-info">Following: ${user.following}</span>
            <br><br>
            <ul class="list-group">
              <li class="list-group-item"><strong>Company:</strong> ${user.company}</li>
              <li class="list-group-item"><strong>Website/Blog:</strong> <a href="${user.blog}" target="_blank">${user.blog}</a></li>
            </ul>
          </div>
        </div>
      </div>`;
      });
      document.querySelector('#output').innerHTML = output;
    }).catch(function (err) {
      document.querySelector('#output').innerHTML = err;
    });
  }

  

TypeError:data.forEach不是javascript中的函数,无法提取   数据表格api

尝试

Object.keys(data).forEach(function (key)
{
....
});

1 个答案:

答案 0 :(得分:0)

您使用的API仅返回对象而不是数组。 来自您的API的响应应如下所示。

{
  "login": "narayansharma91",
  "id": 13073421,
  "node_id": "MDQ6VXNlcjEzMDczNDIx",
  "avatar_url": "https://avatars0.githubusercontent.com/u/13073421?v=4",
  "gravatar_id": "",
}

只需使用而无需如下所示的foreach。

 fetch(`https://api.github.com/users/${username}`).then(function (res) {
    return res.json();
 }).then(function (user) {
  let output =`
    <div class="card card-body mb-3">
    <div class="row">
      <div class="col-md-3">
        <img class="img-fluid mb-2" src="${user.avatar_url}">
        <a href="${user.html_url}" target="_blank" class="btn btn-primary btn-block mb-4">View Profile</a>
      </div>
      <div class="col-md-9">
        <span class="badge badge-primary">Public Repositories: ${user.public_repos}</span>
        <span class="badge badge-secondary">Public Gists: ${user.public_gists}</span>
        <span class="badge badge-success">Followers: ${user.followers}</span>
        <span class="badge badge-info">Following: ${user.following}</span>
        <br><br>
        <ul class="list-group">
          <li class="list-group-item"><strong>Company:</strong> ${user.company}</li>
          <li class="list-group-item"><strong>Website/Blog:</strong> <a href="${user.blog}" target="_blank">${user.blog}</a></li>
        </ul>
      </div>
    </div>
  </div>`;
  document.querySelector('#output').innerHTML = output;
}).catch(function (err) {
  document.querySelector('#output').innerHTML = err;
});