未捕获(在承诺中)TypeError:无法设置 null 的属性“innerHTML”-我可以在 console.log() 中看到值
2020-10-29
566
本项目的目标是开发一个 Web 应用,可用于从
https://api.github.com/users
获取用户并列出其详细信息以及存储库信息。我的
add_user_repos()
函数中的
forEach()
出现问题。该函数在第一次点击时有效,但在第二次点击时无效。以下是我的 Web 应用代码。我收到一个错误:
Uncaught (in promise) TypeError: Cannot set property 'innerHTML' of null'
错误位于粘贴的末尾,注释旁边
<== ERROR HERE
我已经使用了
console.log(repo.name)
并且它打印出来了。
任何关于这个问题或者我的代码的反馈都将不胜感激,提前谢谢了。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Title</title>
<link rel="stylesheet" href="problem_3.css">
</head>
<body>
<div class="center">
<div class="section">
<form name="search_bar">
<label for="search"></label>
<input id="search" type="text" name="search" placeholder="Username">
<button type="button" onclick="search_user()">Search</button>
</form>
</div>
</div>
<div>
<div class="container">
<h1>User Profile</h1>
<div class="wrapper">
<div id="picture">
<!-- image -->
</div>
<div id="user_info">
<table>
<tr>
<th>Name: </th>
<td id="name"></td>
</tr>
<tr>
<th>Username: </th>
<td id="username"></td>
</tr>
<tr>
<th>Email: </th>
<td id="email"></td>
</tr>
<tr>
<th>Locations: </th>
<td id="location"></td>
</tr>
<tr>
<th>Number of gists: </th>
<td id="gists"></td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="container">
<h1>User Repos</h1>
<div class="wrapper">
<div id="user_repos">
<div class="user_repos">
<table id="repo">
<tr>
<th>Name: </th>
<td id="name0"></td>
</tr>
<tr>
<th>Description: </th>
<td id="desc0"></td>
</tr>
<tr>
<th>Name: </th>
<td id="name1"></td>
</tr>
<tr class="row">
<th>Description: </th>
<td id="desc1"></td>
</tr>
<tr>
<th>Name: </th>
<td id="name2"></td>
</tr>
<tr class="row">
<th>Description: </th>
<td id="desc2"></td>
</tr>
<tr>
<th>Name: </th>
<td id="name3"></td>
</tr>
<tr class="row">
<th>Description: </th>
<td id="desc3"></td>
</tr>
<tr>
<th>Name: </th>
<td id="name4"></td>
</tr>
<tr class="row">
<th>Description: </th>
<td id="desc4"></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</body>
<script>
function search_user(){
const url = 'https://api.github.com/users/';
const name = document.forms["search_bar"]["search"].value;
get_user_details(name);
async function get_user_details(input){
const headers = {
//token
}
const resp = await fetch(url + input, {
"method": "GET",
"headers":headers
});
const data = await resp.json();
addData(data);
}
function addData(data) {
const user_name = document.getElementById("name");
const user_username = document.getElementById("username");
const user_email = document.getElementById("email");
const user_location = document.getElementById("location");
const user_picture = document.getElementById("picture");
const user_gists = document.getElementById("gists");
const pic = new Image();
pic.src = data.avatar_url;
user_picture.appendChild(pic);
user_name.innerHTML = data.name;
console.log(data.name);
user_username.innerHTML = data.login;
user_email.innerHTML = data.email;
user_location.innerHTML = data.location;
user_gists.innerHTML = data.public_gists;
get_user_repos(data.login);
async function get_user_repos(username) {
const resp = await fetch(url + username + "/repos");
const repo_data = await resp.json();
add_user_repos(repo_data);
}
function add_user_repos(repos) {
// counter used to find if more than 5 repos are added
let counter = 0;
const repo_div = document.getElementById("user_repos");
repos.forEach((repo) => {
let repo_name = document.getElementById("name" + counter);
let repo_desc = document.getElementById("desc" + counter);
// if more than 5 repo names/desc are added
if (counter > 4) {
const create_repo_name = document.createElement("p");
const create_desc = document.createElement("p");
create_repo_name.innerHTML = "<b>Name: </b>" + repo.name;
create_desc.innerHTML = "<b>Description: </b>" + repo.description;
create_desc.setAttribute("class", "row");
repo_div.appendChild(create_repo_name);
repo_div.appendChild(create_desc);
// add scrolling
document.getElementById("user_repos").style.overflow = "auto";
} else {
repo_name.innerHTML = repo.name; // <== ERROR HERE
repo_desc.innerHTML = repo.description;
repo_name.setAttribute("id", "row" + counter);
repo_desc.setAttribute("id", "desc" + counter);
counter++;
}
})
}
}
}
</script>
</html>
2个回答
不要将节点元素设置为具有“row”+ counter 的 id
repo_name.setAttribute("id", "row" + counter);
将其设置为具有“name”+ counter 的 id
repo_name.setAttribute("id", "name" + counter);
这样,在接下来的搜索中,您将能够再次在 DOM 中找到该元素。
EugenSunic
2020-10-29
TypeError: Cannot set property 'innerHTML' of null'
该问题与
repo_name
变量有关,而不是
repo.name
。
document.getElementById("name" + counter);
返回 null
您应该确保您的 DOM 中存在
"name" + counter
。
A.DUPONCHEL
2020-10-29