Error reporting troubleshooting (variable scope?)

Hi there, this isn’t 100% javascript; I’m working with something within Airtable called “airscript” based on JS but with a bunch of extra functions for getting and writing data from an associated proprietary database-thing. I’m pretty sure the issue is a JS issue though, so here goes:

I’m trying to build a basic error reporting function into a script pulling data from an API. Here’s an abbreviated version of the relevant part of the script:

for (let record of query.records) {
    let registerNumber = record.getCellValueAsString("Register Number");


    if (status == 'Sentenced' && registerNumber) {
        let url = `https://www.bop.gov/PublicInfo/execute/inmateloc?todo=query&output=json&inmateNumType=IRN&inmateNum=${registerNumber}`;
        let response = await remoteFetchAsync(url);
        let apiResult = await response.json();

          if (apiResult && Array.isArray(apiResult.InmateLocator) && apiResult.InmateLocator.length === 1) {
            let inmate = apiResult.InmateLocator[0];
         // *****Do a whole bunch of cool stuff that I'm not including because it's not relevant tot he post****
        } else {
            console.error(`Invalid object returned by api for register number ${registerNumber}: ${apiResult}`);

        }
    }
}

The part I’m having trouble with is the console.error part near the end. Most the script works totally fine. But for some reason when the apiResult doesn’t fit the criteria of the if statements , ${apiResult} returns “[object Object]” instead of the API payload (a common one might be “{“InmateLocator”:[],“Captcha”:false,“Messages”:{},“FormToken”:“pub13029info”}”). Does that mean I’ve got a scope issue with the variable, or is there some other reason that’s what’s coming back?

Thank you!

Just at a quick glance, if that is coming back as an object, why not do something like JSON.stringify(apiResult) to convert it to a string so you can at least see what it is.

The other thing I might log out is:

  console.error(Array.isArray(apiResult.InmateLocator));
  console.error(apiResult.InmateLocator);

I’d want to do that so I can see what is happening.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.