I am using API with javascript for first time, so I am using jsonplaceholder fake API.
I have an array of objects and I am trying to iterate over a property in this array of object but I got this error:
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'appendChild')
Here is my code
const api_url = 'https://jsonplaceholder.typicode.com/users';
let specialisty = document.getElementById('specialisty-menu');
async function getUser() {
// Making an API call (request)
// and getting the response back
const response = await fetch(api_url);
// Parsing it to JSON format
const data = await response.json();
console.log(data);
let name = data.map((a) => a.name);
console.log(name);
for (let i = 0; i < name.length; i++) {
console.log(name[i]);
let item = document.createElement('a');
item.appendChild(document.createTextNode(name[i]));
specialisty.appendChild(item);
}
}
getUser();
<div id="specialisty-menu"></div>