Typeerror: Data.Foreach Is Not a Function

Uncaught TypeError: data.forEach is not a function at XMLHttpRequest.request.onload

The data at the URL https://restcountries.eu/rest/v2/alpha/col is not an array, it is a JavaScript object. If you want to traverse it's properties with forEach you have to call something like Object.keys or Array.from on the parsed data, like

Object.keys(data).forEach((k) => {  
arr.push(data[k].name)
})


res.data.forEach is not a function in Jquery

Ah, I just realised. Look at your res.data and you need res.data.data. Replace your code with:

let totalData = [];
let monthData = [];

$(async function () {
await axios.get(`/dashboard/chart/month`).then((res) => {
console.log(res.data);

// Add another data here:
res.data.data.forEach((data) => {
totalData.push(data.total);
monthData.push(data.months);
});

});

});

The res.data is the Axios's version. It has your data + status and other stuff. You have your data as well. So you need res.data.data.



Related Topics



Leave a reply



Submit