samdaily34
samdaily34

Reputation: 67

Type Error: Cannot Read Property "x" from undefined. Only when I include an array of values

I am trying to loop through values from a google sheet column that first runs through a function to parse a response on a json page to get a unique identifier, then another json response using the unique identifier to find an attribute's value (true or false).

I have tried a "for loop" that I will include in my code

function main(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Pat ID');
  var zdPatIDs = sheet.getRange("A2:A").getValues();

  for (id in zdPatIDs){
    var ptArray = getZDUser(zdPatIDs[id])
    // var updateProfile = coldChainOffered(ptArray)
    var response = getResponse(ptArray.name);
  }
}

// returns requestor ID from value of patients in google sheet
function getZDUser(zdPatIDs){
  var url = 'https://someURL.com/api/v2/search.json?query='+zdPatIDs
  var options =
   {
     "method": "get",
     "contentType" : "application/json",
     "headers": 
     {
         "Authorization": "Basic " + Utilities.base64Encode("bearer/token:string of numbers")
     },
     "muteHttpExceptions": true
  }

  var response = UrlFetchApp.fetch(url, options)
  var response = JSON.parse(response)
  return response.results[0].requester_id
}

// returns json cold chain offered response from requester id function
function getResponse(ptArray){
  var url = 'https://someURL.com/api/v2/users/' + ptArray + '.json'
  var options =
    {
      "method": "get",
      "contentType" : "application/json",
      "headers": 
      {
        "Authorization": "Basic " + Utilities.base64Encode("bearer/token:string 
   of numbers")
      },
      "muteHttpExceptions": true
  }

  var response = UrlFetchApp.fetch(url, options)
  var response = JSON.parse(response)
  return response.user.user_fields.cold_chain_offered
}

When I only have one value and no for loop the correct response is returned, however when there are multiple I get the type error thrown

Upvotes: 2

Views: 165

Answers (1)

computercarguy
computercarguy

Reputation: 2453

You have a lot going on, and not all of it is supplied. Including a minimal example of your JSON would be good to help us figure out what's going on, but I'll see if I can cover some other issues I see.

First off, re-declaring response may be part of the issue. It's not good programming practice to re-declare variables and it'll even fail your program in other languages. You are doing this at least 2x in your example, so please fix that.

Next, you should generally null check your objects before trying to use them. If your response is an empty string, empty JSON, or simply doesn't have that key, you'll get an error. Including the error(s) in your Question will also help us figure out what problem(s) you're having.

Also, formatting your code to have less on each line will help you figure out what parameters you are sending and if they have any errors that are causing you to not get the data you expect. (I'll edit the Question here is a sec to show you what I mean.) Using a coding standard might be useful, especially if you are coding professionally or have peers/partners that will look at your code.

Using Fiddler, or something similar will help you figure out what exactly you are getting for returned data, so you can try to figure out what's "not right" with your code.

And lastly, looking at the JSON.parse documentation might help you figure out if the JSON you are getting is correctly formatted and if it has anything else that doesn't conform to the expected standards. If you are writing the server side of things, the difference between using a ' and " is crucial in JSON, as I've found out the hard way.

https://www.w3schools.com/js/js_json_parse.asp

Upvotes: 1

Related Questions