javascript - Axios 调用后无法附加 div
问题描述
我查看了以前在这个论坛上提出的问题,但没有找到与这种特定情况相关的任何内容。我正在尝试根据从 Github 提取的数据创建用户卡。当我尝试将第一个子 div 附加到 HTML 中存在的父 div 时,我的代码会引发以下错误“TypeError:无法在 'Node' 上执行 'appendChild':参数 1 不是 'Node' 类型。” 问题是它绝对是一个节点。这是我的代码。
//Axios call
const info = axios.get(url).then(({ data }) => createCard(data));
function createCard(gitHubData) {
const cards = document.querySelector(".cards");
//Creating the elements for the cards
const card = document.createElement("div").classList.add("card");
var img = document.createElement("img");
const info = document.createElement("div").classList.add("card-info");
var name = document.createElement("h3").classList.add("name");
var username = document.createElement("p").classList.add("username");
var location = document.createElement("p");
var profile = document.createElement("p");
var followers = document.createElement("p");
var following = document.createElement("p");
var bio = document.createElement("p");
//Creating the nested layout of our elements
cards.appendChild(card); //code fails here
card.appendChild(img);
card.appendChild(info);
info.appendChild(name);
info.appendChild(username);
info.appendChild(location);
info.appendChild(profile);
info.appendChild(followers);
info.appendChild(following);
info.appendChild(bio);
//Assigning the cards to info collected from axios
img.src = gitHubData.avatar_url;
console.log(img.src);
name = gitHubData.name; //If I use .innerHTML or .textContent returns
//undefined
console.log(name);
username = gitHubData.login;
location = gitHubData.location;
profile = `Profile: ${gitHubData.html_url}`;
followers = gitHubData.followers;
following = gitHubData.following;
bio = gitHubData.bio;
return cards;
}
解决方案
我仍然不确定为什么我的代码一开始就失败了,但是我晚上关闭了计算机,然后在早上再次尝试。正确的代码(我之前尝试失败的代码如下:
name.textContent = gitHubData.name;
username.textContent = gitHubData.login;
location.textContent = gitHubData.location;
profile.textContent = `Profile: ${gitHubData.html_url}`;
followers.textContent = gitHubData.followers;
following.textContent = gitHubData.following;
bio.textContent = gitHubData.bio;
推荐阅读
- android - 为什么在使用自定义背景为 textview 充气时会出现错误?
- ios - 如何在 Plist 中获取捆绑版本?
- python-3.x - Python Flask Blueprint 向全局所有上下文注入变量值?
- c# - 如何在 ASP.NET 中使用 OpenXML 在 PPT 幻灯片中创建散点图
- github - 无法在 github 工作流程中使用 npm cli
- graphql - graphql get 查询显示此错误“标量中不允许使用变量”
- javascript - Toast ui(tui) datepicker 如何重置更改日期?
- javascript - 悬停在可点击
- java - 无法在 gradle 中为 java 应用程序运行不同的类文件
- java - 如何用间谍替换现有的bean