Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Unable to make doPost(e) work - Cannot read properties of undefined

210 views
Skip to first unread message

Álvaro Castilla Vera

unread,
Oct 27, 2024, 7:31:20 AM10/27/24
to Google Apps Script Community
Hello, I'm trying to send the whatsapp number from the sender of a message to my twilio account and I'm trying to extract it to a spreadsheet. (Additionally to compare the row that has it and write in another column the word 'CALL')

My problem lies in the fact that I'm getting this error each time I triy to send the HTTP POST request from my twilio flow to my google app script via webhook. The webhook is correctly set up and does connect.

Here is the error:

Error: Cannot read properties of undefined (reading 'channel') - This is the response I'm getting for the request in Twilio. I assume it isn't defining e as the json I'm sending to app script.

This is my code in google app script:

function doPost(e) {
  var sheet = SpreadsheetApp.openById('for confidentiality I cant share this').getSheetByName('HIPOTECAS'); // Obtener la hoja llamada "HIPOTECAS"
  var data = JSON.parse(e.postData.contents);
 
  var phoneNumber = data.num; // Asume que el número de teléfono viene en la propiedad 'phoneNumber'
 
  // Buscar la fila que contiene el número de teléfono
  var range = sheet.getDataRange();
  var values = range.getValues();

  for (var i = 0; i < values.length; i++) {
    if (values[i][P-1] === phoneNumber) { // Cambia P por el número de columna donde están los números (0-indexed)
      sheet.getRange(i + 1, 24).setValue('LLAMAR'); // Columna X es la número 24 (0-indexed)
      break; // Salir del bucle si se encuentra el número
    }
  }

  return ContentService.createTextOutput('Success');
}

Andrew Apell

unread,
Dec 10, 2024, 10:53:09 PM12/10/24
to Google Apps Script Community
dPost in interesting. I have some scripts that skip this step:

var data = JSON.parse(e.postData.contents)

and instead access the properties directly by using e.parameter,<some_property_name>

Another strategy that would help is for you to use optional chaining as much as possible. That way, any property that does not exist will not cause the whole script to error out. This particular solution recently rescued me from serious frustration with a payment processor's webhook.

Finally, you could try logging the entire doPost event in order to see what your script is receiving instead of making assumptions about it.
Reply all
Reply to author
Forward
0 new messages