JavaScript

MIHA WANG
MIHA WANG
Front End Web Development Techdegree Student 10,820 Points

Practice Fetch API

Hey guys!! I am practice my Fetch API skills. There is some problem, I followed the tutorial, but every time i fetch the api it gets undefined. I dont know why is keep happening, hope you guys can help me out, thank you so much!!! Code as below:

var result = document.getElementById('result'); fetch('https://portal.iclass.hk/devtest.php') .then(data => data.json()) .then(results => logOut(results));

function logOut(x) { if(x.employee_age <= 25) { result.innerHTML = x.employee_name; } }

1 Answer

Piotr Manczak
Piotr Manczak
Front End Web Development Techdegree Student 16,879 Points

Maybe try using asynch/await. I had similar problem recently. I could not access an array. It turned out that I was attempting to do that before an array was ready.