I have below scripts on Google Apps Script which will take in an JSON event.
I want the data in element “events”, but it always return “Undefined” to me.
I’m guessing maybe it’s because events is a JSON array and I can’t directly use it in JS?
Here is my code:
function doPost(e) {
var msg = JSON.parse(JSON.stringify(e.postData.contents));
console.log(msg);
//log succesfully
console.log(msg.events);
//"Undefined"
}
If I try to log the elements in the events instead of the array events itself:
console.log(msg.events[0].replyToken);
//"TypeError: Cannot read property '0' of undefined at doPost(app:26:25)"
The result of msg will is in below:
{
"destination": "U656de3c6b48c8e0d44edda4fd3f05e06",
"events": [
{
"type": "message",
"message": {
"type": "text",
"id": "*********",
"text": "Hi"
},
"timestamp": 1642616050062,
"source": {
"type": "group",
"groupId": "***********",
"userId": "*************"
},
"replyToken": "************",
"mode": "active"
}
]
}
I’ve seen several similar problems for array in JS.
I tried them but all of them didn’t work, please help me.