0

I'm using ChartJS to build a line graph. The data is coming from an API. I take the data points from the API and use the push method to build an array to be used in the parameters of ChartJS data: { data: dataFromAPI. I'm using AJAX to GET data from the API.

When I do this:

var someArray = [23,49,39,59];

and input it into the data: { data: parameter, it works. In the console, someArray looks like this

Array(4) [23,49,39,59]

When I use the push method in the AJAX call, the array looks like this in the console:

Array []

Both array variables are being declared globally.

The array that I'm using the push method to build in the AJAX call does not work with ChartJS. It produces this error:

Uncaught TypeError: n[a]._view is undefined

What is the difference between, what I would refer to as a manually typed array, and the array that push built? And how come ChartJS does not want to accept push array?

I would also like to note that Array [] does have elements inside.

LaravelSucks
  • 131
  • 2
  • 8
  • 1
    Have you thought about building up an array using push(), and then assigning the whole array to your chartObject.data.data ? I don't know why this is happening, but could be to do with how the accessor is exposed for the "data" field. – Jack Dec 17 '20 at 03:18
  • I will try this, thank you. – LaravelSucks Dec 17 '20 at 03:23
  • Did not work. Although it cleared the errors in the console, the chart remains data-less. – LaravelSucks Dec 17 '20 at 03:31
  • @LaravelSucks can you share your code? might be easier for people to understand your problem that way. – rumman0786 Dec 17 '20 at 04:12

1 Answers1

1

I completely neglected the asynchronous nature of AJAX, and did not enclose my script in an async call. This stackoverflow question/answer provided me with enough insight to move on. It should be noted that global arrays behavior differently when they are assigned values inside a scoped function.

LaravelSucks
  • 131
  • 2
  • 8