TypeError: Cannot read property '0' of undefined – Google app script

I have a code that gets dates and saves them and an array so that it’s used in another function to make a POST request. I am having a challenge passing these dates into a post requestand keep getting an error Cannot read property ‘0’ of undefined. I will appreciate help with this.

Dates code:

function getDate(){

 var dateArray = [];
 var date1= new Date(2021,2,15);
 var date2 = new Date(2021,3,15);

 Set the current date to the starting date 
 var date = date1; 
 
 while(date<=date2){

   dateArray.push(date);
   date.setDate(date.getDate()+1);
 }

 return(dateArray);
}


function user_assignments()
{
  var user = getUsers()

  var assign_arr = [];

  for (var i = 0; i < user.length; i++)
  {
    var url = 'https://api.10000ft.com/api/v1/users/' + user[i].ID + '/assignments?auth=' + tkft_token + '&from=' + start + '&to=' + end
    var response = UrlFetchApp.fetch(url, options);
    var info = JSON.parse(response);
    var content = info.data;


    if (content.length > 0)
    {
      for (var j = 0; j < content.length; j++)
      {
        var rec = {}
        rec.user_id = content[j].user_id
        rec.assignable_id = content[j].assignable_id
//        rec.ends_at = content[j].ends_at
        assign_arr.push(rec)

      }
    }


  }

  return assign_arr;
}

POST Request code:

function clear_demo(date)
{
var user_dt = user_assignments()
var usedates = getDate()

for (var i = 0; i < user_dt.length; i++)
  {

     var data = {

      'hours': 0,
      'date': usedates[i],//Having challenge here
      'user_id': user_dt[i].user_id,
      'assignable_id': user_dt[i].assignable_id,
    };
    var payload = JSON.stringify(data);
    var options = {
      'method': 'POST',
      'Content-Type': 'application/json',
      'payload': data,
    };

11 thoughts on “TypeError: Cannot read property '0' of undefined – Google app script”

Leave a Comment