0

Im trying to create a chart using charts.js with an array of data I am getting from an ajax call. VisitsPerDay is an array i created and i am pushing the values from the ajax call

visitsPerDay = visitsPerDay.push(Object.values(html.results[0])[0])

Then if i console.log it i get this enter image description here

When I use these 3 commands

  console.log("PerDay", visits)
  console.log("PerDay", visits[0])
  console.log("PerDay", visits.length)

I should get the array, the first number, and length, right? Instead I'm getting the array,undefined and 0

enter image description here

Strike
  • 77
  • 1
  • 7
  • try `console.log("PerDay", visitsPerDay ) console.log("PerDay", visitsPerDay[0]) console.log("PerDay", visitsPerDay.length)` also you need not assign the push `visitsPerDay.push(Object.values(html.results[0])[0])` this should be the first line – Naren Murali Aug 23 '22 at 10:17
  • Don't store the results of the push [which is the new length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push). Just `visitsPerDay.push(Object.values(html.results[0])[0])` – mplungjan Aug 23 '22 at 10:18
  • Does this answer your question? [Weird behavior with objects & console.log](https://stackoverflow.com/questions/23429203/weird-behavior-with-objects-console-log) – Ivar Aug 23 '22 at 10:20
  • Thanks guys i will test it and answer in a bit!! – Strike Aug 23 '22 at 10:24
  • @NarenMurali visits its the same as visitsPerDay just passed on a `function createWeekChart(visitsPerDay)` – Strike Aug 23 '22 at 10:25
  • @Ivar nop Still nothing. – Strike Aug 23 '22 at 10:41
  • @Strike What do you mean by "still nothing"? The post I linked explains why the array _appears_ to contain elements, while in reality it doesn't at the moment it is logged. – Ivar Aug 23 '22 at 10:58
  • @Ivar my bad. I mean i tried the solutions from the post and still cannot access or show what is inside my array to use it. – Strike Aug 23 '22 at 11:03
  • @Strike You cannot access it because at the point you try to access it, it doesn't exist yet. The post I linked explains why it _appears_ it does. It doesn't have an explanation on how you can fix it, because it is not part of the question. Also for your question it is the only answer we can give you. We can't explain why this data isn't available at the point of logging, because you haven't shown how the array is being filled. We will need a [mcve] to be able to help you any further. – Ivar Aug 23 '22 at 11:06
  • @Ivar Here is a codepen with the code u need.[link](https://codepen.io/rafaelceng/pen/NWYJQme?editors=11110) The only thing that is missing is the database query i am doing in my backend where i access my database and retrieve the data i need. first time doing a minimal reproducible example i hope is decent. – Strike Aug 23 '22 at 11:42
  • 1
    @Strike Your `visitsPerDay.push()` is inside the AJAX `success` callback. This callback is invoked when the response from the AJAX request has been received and this works asynchronous. So the `weekVisits()` function will complete without changing `visitsPerDay` and all the other synchronous code will execute, _before_ the `success` callback is executed. It's a common problem. See [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ivar Aug 23 '22 at 11:48
  • @Ivar I know about asynchronous. I even made my ajax call synchronous to test it but still which should I believe with the logic? But still, it was not working. I know practically making it synchronous is a bad idea. But performance its not what I'm focusing. Nevermind i made a crucial mistake I was putting async: false inside "". It's working but it is synchronous currently. – Strike Aug 23 '22 at 12:02
  • Synchronous AJAX is not something you want. It's a deprecated property in jQuery and many browsers have deprecated [the underlaying `async` parameter](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open#syntax) as well. The link in my previous comment explains ways on how you can resolve that issue. – Ivar Aug 23 '22 at 12:07
  • @Ivar upvoted your comment. Managed to make it asynchronous.Thanks!! – Strike Aug 24 '22 at 07:05

0 Answers0