Dynamically update html content plain JS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Dynamically update html content plain JS



So I have a script bound in my index.html, which literally has to fetch an array of jsons from an api, then dynamically update my page when get response.



But as a result I'm getting an as the only update of my page.



here is my script


fetch(url)
.then((resp) => resp.json())
.then((resp) =>
resp.results.forEach(item =>
fetch(item.url)
.then((response)=> response.json())
.then((response) => pokeArr.push(response))
.catch(console.log)
)
).catch(console.log)


const filler = () =>
if(!pokeArr) return 0
pokeArr.map((i,j)=>
return `<tr>$i.name</tr>`
)


const pokeindex = () =>
document.getElementById('a').appendChild(document.createElement(filler()))

pokeindex()



When I'm consoling it, I can see in the console all the responses I get, so I'm at least doing the fetching part right.





What's parberakan? What's pokeArr?
– T.J. Crowder
Aug 8 at 12:56


parberakan


pokeArr





Note that document.createElement doesn't accept HTML strings. It accepts the tag name of the single element to create (e.g., document.createElement("div")).
– T.J. Crowder
Aug 8 at 12:56


document.createElement


document.createElement("div")





Side note: Your fetch call is missing an .ok check, details in this post on my anemic little blog.
– T.J. Crowder
Aug 8 at 12:56


fetch


.ok





parberakan is the old name of the pokeArr, which is an empty array, fixed that in my code, nothing changed
– Lyudvig Grigoryan
Aug 8 at 12:57





Also note that nothing is waiting for your multiple fetch calls to complete before trying to do something (I think?) with the results.
– T.J. Crowder
Aug 8 at 12:58


fetch




1 Answer
1



There are a number of issues here:



You have parberakan in one place but pokeArr in another; in a comment you've suggested they're meant to be the same thing.


parberakan


pokeArr



You don't declare parberakan/pokeArr anywhere in the quoted code.


parberakan


pokeArr



Nothing in the quoted code is waiting for the various fetch calls to complete (not even the first, and esp. not the series of them in one of its then handlers).


fetch


then



The fetch calls aren't checking .ok; see the post on my anemic little blog.


fetch


.ok



document.createElement doesn't accept HTML strings. It accepts the tag name of the single element to create (e.g., document.createElement("div")).


document.createElement


document.createElement("div")



filler doesn't return the result of map.


filler


map



Passing console.log directly to catch isn't 100% reliable.


console.log


catch



At a rough guess, I suspect you want to do something like this (see comments), but I do recommend stepping back from your current task and working through some basic tutorials:


// Create these functions first
const filler = (pokeArray) => // Accept the array as a parameter
if(!pokeArray) return null; // Recommend null instead of 0 if the other return is an array
return pokeArray.map((i,j)=> // Return the result of `map`
return `<tr>$i.name</tr>`;
);
;

const pokeindex = (pokeArray) => // Accept the array as a parameter
// I'm assuming the element with id="a" is a `table` or `tbody`
document.getElementById('a').insertAdjacentHTML( // This accepts HTML
"beforeend",
fillter(pokeArray)
);


fetch(url)
.then((resp) =>
// Check .ok
if (!resp.ok)
throw new Error("HTTP error " + resp.status);

return resp;
)
.then((resp) => resp.json())
.then((resp) => Promise.all(resp.results.map(item => // Wait for all these to complete;
// result is an array of the
// resolution values
return fetch(item.url)
.then((response) =>
// Check .ok
if (!response.ok)
throw new Error("HTTP error " + resp.status);

return response;
)
.then((response) => response.json());
)))
.then((pokeArray) =>
// Now we have the array and can do something
pokeindex(pokeArray);
)
.catch(error => console.log(error); ); // Passing `console.log` directly isn't reliable



I may not have caught every last little thing, though. This is just to help get you pointed the right way.





The question is probably far too broad, though. I've voted to close, recommend others do the same.
– T.J. Crowder
Aug 8 at 13:16






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Creating a leaderboard in HTML/JS