Chartjs-Cannot read property 'map' of undefined when looking at an array with no index

0👍

Going to throw out an answer based on our comment discussion, it will be easier to explain where your issue is.

What is being returned in the parameter ‘res’ is an array. Arrays have a access to a method called map. Map takes a function, and will call the function once for each value in the array and overall return a new array with the return values of map.

ie:

var res = [{ username: 'jsmith', firstname: 'John' }, { username: 'jdoe', firstname:' Jane" }];

var newArray = res.map(user => user.firstname);
console.log(newArray) // ['John', 'Jane']

Based on the mapping function, the first name will be grabbed from each object in the array, and a new array with those values will be created.

Arrays can also be accessed by integer index.
So:

var john = res[0];
console.log(john) // { username: 'jsmith', firstname: 'John' }

So in your case, you can just do res.map and you would get a each value one by one handed to that callback, and you could process them. Alternatively, you could access the elements by index res[0].id for example.

you can not do res[0].map , because the object stored in the first spot of the response is not an array

Leave a comment