[英]My data from my api won't display in my html (im still new to coding but here is my code)
var listApi = document.querySelector(".list-item")
var searchApi = document.getElementById("search-api")
document.querySelector("#enter").addEventListener("click", e => {
e.preventDefault();
var inputValue = searchApi.value;
fetch('https://wft-geo-db.p.rapidapi.com/v1/geo/cities/' +inputValue, options)
.then(response => response.json())
.then(response => console.log(response))
.then((data) => {
var title = document.createElement("li")
title.setAttribute = ".list-item";
title.innerText = data;
})
.catch(err => console.error(err));
});
//Html portion //HTML部分
<h2 class="justify-center flex">Results</h2>
<ul id="result-list">
<li class="list-item "></li>
</ul>
The api works fine as it displays the data in the console, i just can figure how to get it in my html. api 在控制台中显示数据时工作正常,我只是想知道如何在我的 html 中获取它。
When you create an element with document.createElement
it exists only in memory-- it isn't actually attached to the DOM yet.当您使用document.createElement
创建一个元素时,它只存在于内存中——它实际上还没有附加到 DOM。 You need to insert it somewhere, like so:您需要将它插入某处,如下所示:
var listApi = document.querySelector(".list-item")
var searchApi = document.getElementById("search-api")
document.querySelector("#enter").addEventListener("click", e => {
e.preventDefault();
var inputValue = searchApi.value;
fetch('https://wft-geo-db.p.rapidapi.com/v1/geo/cities/' +inputValue, options)
.then(response => response.json())
.then(response => console.log(response))
.then((data) => {
var title = document.createElement("li")
title.setAttribute = ".list-item"
title.innerText = data;
var list = document.getElementById("result-list");
list.appendChild(title);
})
.catch(err => console.error(err));
});
Also note your line title.setAttribute = ".list-item"
won't work as you expect-- you are overwriting the setAttribute
function with a string.另请注意,您的行title.setAttribute = ".list-item"
不会像您预期的那样工作——您正在用字符串覆盖setAttribute
function。 Better to just use classList
as title.classList.add('list-item');
最好只使用classList
作为title.classList.add('list-item');
Also , as user Andy points out in the comments, you have another problem with your chaining of .then
s-- specifically, you have a .then()
that console.log
s the result and returns nothing.此外,正如用户 Andy在评论中指出的那样,您的.then
链接还有另一个问题——具体来说,您有一个 .then .then()
console.log
是结果并且不返回任何内容。 The way promise chains work is that the next .then
will act on the result passed from the previous .then
; promise 链的工作方式是下一个.then
将作用于前一个.then
传递的结果; however, .then(response => console.log(response))
will return undefined
, so the data
argument coming into your next .then
will be undefined
.但是, .then(response => console.log(response))
将返回undefined
,因此进入下一个.then
的data
参数将是undefined
。 Below is a code example that fixes both the setAttribute
issue and the .then
issue:下面是修复setAttribute
问题和.then
问题的代码示例:
var listApi = document.querySelector(".list-item")
var searchApi = document.getElementById("search-api")
document.querySelector("#enter").addEventListener("click", e => {
e.preventDefault();
var inputValue = searchApi.value;
fetch('https://wft-geo-db.p.rapidapi.com/v1/geo/cities/' +inputValue, options)
.then(response => response.json())
.then(response => {
console.log(response);
return response;
})
.then((data) => {
var title = document.createElement("li")
title.classList.add("list-item");
title.innerText = data;
var list = document.getElementById("result-list");
list.appendChild(title);
})
.catch(err => console.error(err));
});
Finally, if you are just attempting to insert a plain object or array as text into the DOM you will likely get unexpected results, such as it displaying simply as "object Object"
in the <li>
.最后,如果您只是试图将一个普通的 object 或数组作为文本插入到 DOM 中,您可能会得到意想不到的结果,例如它在<li>
中仅显示为"object Object"
。 Let's presume for a moment that your response looks something like this:让我们暂时假设您的响应如下所示:
{
data: ['MacReady', 'Childs', 'Blair', 'Nauls', 'Clark', 'Palmer']
}
To write this data to the DOM, you'd need to access it at the data
property, then map over it (either with a loop or using an array method like .forEach
) and add each item to an element (like an <li>
in your case) and insert it to the DOM.要将此数据写入 DOM,您需要在data
属性中访问它,然后在其上访问 map(使用循环或使用数组方法,如.forEach
)并将每个项目添加到一个元素(如<li>
在你的情况下)并将其插入 DOM。 Here's an example:这是一个例子:
var listApi = document.querySelector(".list-item")
var searchApi = document.getElementById("search-api")
document.querySelector("#enter").addEventListener("click", e => {
e.preventDefault();
var inputValue = searchApi.value;
fetch('https://wft-geo-db.p.rapidapi.com/v1/geo/cities/' +inputValue, options)
.then(response => response.json())
.then(response => {
console.log(response);
return response;
})
.then((data) => {
let myList = data.data;
myList.forEach(datum => {
var title = document.createElement("li")
title.classList.add("list-item");
title.innerText = datum;
var list = document.getElementById("result-list");
list.appendChild(title);
});
})
.catch(err => console.error(err));
});
There are other approaches to this-- using a for
loop, or using DOM fragments to increase performance, but something along these lines should work for your use case.还有其他方法——使用for
循环,或使用 DOM 片段来提高性能,但这些方法应该适用于您的用例。
The only missing part is how you loop over your data and add items to your list, so here's a quick example.唯一缺少的部分是如何遍历数据并将项目添加到列表中,所以这是一个简单的示例。
// Cache the list element const ul = document.querySelector('ul'); // Your data will look something like this // where `data` is an object with a property // with an array const data = { cities: [ { name: 'London' }, { name: 'Paris' } ] }; // Once your data has returned, loop // over the array, and then add each new item // to the list for (const city of data.cities) { const item = document.createElement('li'); item.textContent = city.name; ul.appendChild(item); }
<ul></ul>
If you wanted another approach that uses more "modern" methods:如果您想要另一种使用更“现代”方法的方法:
// Cache the list element const ul = document.querySelector('ul'); // Your data will look something like this // where `data` is an object with a property // with an array const data = { cities: [ { name: 'London' }, { name: 'Paris' } ] }; // Create an array of HTML strings by mapping // over the data const html = data.cities.map(city => { return `<li>${city.name}</li>`; }).join(''); // And then adding that joined string to the list ul.insertAdjacentHTML('beforeend', html);
<ul></ul>
Addition information添加信息
You need to update an element that is in the dom.您需要更新 dom 中的元素。
var listApi = document.querySelector(".list-item")
var searchApi = document.getElementById("search-api")
document.querySelector("#enter").addEventListener("click", e => {
e.preventDefault();
var inputValue = searchApi.value;
fetch('https://wft-geo-db.p.rapidapi.com/v1/geo/cities/' +inputValue, options)
.then(response => response.json())
.then(response => console.log(response))
.then((data) => {
listApi.innerText = data;
})
.catch(err => console.error(err));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.