0

This was working fine but has for some reason stopped, I'm fetching a JSON string from an mvc controller action, which according to console.log is returning fine, however actually trying to use the array of objects returns undefined. This has nothing do to with Ajax being asynchronous as the returned data is there it's just not usable.

The ajax call & chart function:

    $(document).ready(function () {
                    $.ajax({
                        url: '/Home/GetSessionGraphData',
                        type: 'POST',
                        success: function (result) {
                            console.log(result);
                            $.each(result.GraphData, function (stat) {
                                addData(myChart, stat.Time, stat.Stat)
                            });
                        },
                        error: function () {
                        }
                    });

    function addData(chart, label, data) {
                    chart.data.labels.push(label);
                    chart.data.datasets.forEach((dataset) => {
                        dataset.data.push(data);
                    });
                    chart.update();
                }

The controller function:

    public JsonResult GetSessionGraphData()
            {
                return Json(new { _api.GraphData }, JsonRequestBehavior.AllowGet);
            }

My console.log inside the success:

{GraphData: Array(2)}
GraphData: Array(2)
0: {Time: "9:35", Stat: 0}
1: {Time: "9:40", Stat: 1001}

However trying to actually use that data is returning undefined.

Coffee
  • 392
  • 3
  • 16
  • 1
    Which object is it suggesting is undefined? Also, where are you defining `myChart`? – Diado Jul 15 '19 at 08:50
  • Where is myChart defined like @Diado said. – Thameem Jul 15 '19 at 08:56
  • the chart is just a standard chartJS chart being defined a few lines above, didn't include it in my question because the chart works fine (aside from the labels saying "undefined" and no numbers being added). @Diado the stat.Time and stat.Stat are undefined, if i console.log(stat) it just returns the index of the object and nothing else. – Coffee Jul 15 '19 at 08:58
  • That's because you've only specified a single argument for the callback you're passing to `$.each()`. See [the documentation](https://api.jquery.com/jquery.each/) – Diado Jul 15 '19 at 09:01
  • @diado that fixed it thanks, any idea why when I was last working on this on Friday it worked completely fine without the second argument? – Coffee Jul 15 '19 at 09:04
  • if this doesn't has anything to do with ajax, please try to reproduce the issue without ajax, with a static data stub, and provide a [mcve]. If it's not about ajax, it's either front-end only or back-end only issue – YakovL Jul 15 '19 at 11:58
  • 1
    @YakovL It was a front-end issue, this line: $.each(result.GraphData, function (stat) needed to be $.each(result.GraphData, function (index, stat) The issue was me not understanding how .each() worked properly – Coffee Jul 15 '19 at 12:10

0 Answers0