0

I'm wondering if you can help me. I have some code that works that I have been trying to restructure. This is how it looks now:

function myfunction(ref) {
  getReport(ref, "queue", "hour", "2018-09-03", "2018-10-04", "pageviews", "page").done(function(r1) {
    getReport(r1.reportID, "get").done(r2=>reportHandler(r1.reportID, r2, 0))
  });
}

function reportHandler(id, r2, retries){
    if(retries >= 3){
        console.log("Retried 3 times - stopping")
        return
    }
    if (r2.error == "report_not_ready") {
        console.log("Not ready");
        setTimeout(function() {
          getReport(id, "get").done(r2=>reportHandler(id, r2, retries + 1))
        }, 2000);
      }
      console.log(r2);
}

function getReport(ref, type, granularity, from, to, metric, element) {
  return $.getJSON("report.php", {
    ref: ref,
    type: type,
    granularity: granularity,
    from: from,
    to: to,
    metric: metric,
    element: element,
  });
}

What I haven't been able to figure out is how to process the data, which I want to be able to do in myfunction.

At the moment the only way I can do is return the data in my report handler function.

I want to be able to return the data from the API in my myfunction function where I will then process it further, and then keep my reporthander and getreport functions generic.

2
  • Use then instead of done to pipe outputs. Commented Oct 9, 2018 at 15:49
  • @plalx It's a good idea but seems I'm not using the propert syntax for that? Commented Oct 9, 2018 at 17:13

1 Answer 1

1

I believe your problem resides here:

getReport(r1.reportID, "get").done(r2=>reportHandler(r1.reportID, r2, 0))

$.getJSON() functions as a promise, with .done() being identical in functionality to .then(). Both of these accept a function as an argument, but the way you're passing it is not proper syntax. By passing the function with parameters instead of just its name, you're telling JavaScript to run the function and use its results - which isn't what you want. In your case, the simplest fix will be:

getReport(r1.reportId, "get").done((r2) => { reportHandler(r1.reportId, r2, 0); });

Alternately, if you're able to use the new async/await syntax, you could make it simpler:

const r2 = await getReport(r1.reportId, "get");
const handler = await reportHandler(r1.reportId, r2, 0);
// and so on

Does this make sense? Please let us know if there's anything else you don't understand.

EDIT: Okay, so per the comments on this answer, the code being run is correctly structured as:

function myfunction(ref) {
  getReport(ref, "queue", "hour", "2018-09-03", "2018-10-04", "pageviews", "page").done(function(r1) {
    getReport(r1.reportId, "get").done((r2) => {
      reportHandler(r1.reportId, r2, 0);
    });
  });
}

The only answer I can think, then, is that the first getReport() call within myFunction() is not returning an object with reportId in the body. Are you getting any uncaught errors when this code runs?

EDIT 2: Per comments, the attribute was being incorrectly referenced due to a capitalization error. The code was calling for r1.reportId when the response was returning r1.reportID.

Sign up to request clarification or add additional context in comments.

9 Comments

Hello, thank you for this. I'm trying to put it together and it looks like this: function myfunction(ref) { getReport(ref, "queue", "hour", "2018-09-03", "2018-10-04", "pageviews", "page").done(function(r1) { getReport(r1.reportId, "get").done((r2) => { reportHandler(r1.reportId, r2, 0); }); }); } but it doesnt seem to pass the report ID across
Have you confirmed that your getReport() function is returning the right results? You can do this by putting console.log(r1); in the callback, ahead of your second getReport() call. Namely, I'm wondering whether the first call is returning an object with a reportId attribute.
What you have written in the comments is correct. Your original post is missing the curly brackets around reportHandler(), so I wasn't sure if you had those in your original code or not. Check your code to make sure they're there. Editing my answer here in a second.
Actually, I think I've found the issue one second
It had to have a capital D in reportID that was the only issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.