K
K
KasperMrrr2020-08-08 16:10:35
Node.js
KasperMrrr, 2020-08-08 16:10:35

How to fix "TypeError: Cannot read property 'getItemResponses' of undefined (line 7, file Code)"?

I found a script for DS so that answers from Google forms would fall into the discord channel, but it gives such an error. Here is the script itself:

var POST_URL = "https://discordapp.com/api/webhooks/741634503621214220/5M3tWW_2I-Kk_BpDvYnFsmheKt7q1PafpiROkF83eJgMpOUdb9jcNJTKBryZz5-ejmFO";

function onSubmit(e) {
    var form = FormApp.getActiveForm();
    var allResponses = form.getResponses();
    var latestResponse = allResponses[allResponses.length - 1];
    var response = latestResponse.getItemResponses();
    var items = [];

    for (var i = 0; i < response.length; i++) {
        var question = response[i].getItem().getTitle();
        var answer = response[i].getResponse();
        try {
            var parts = answer.match(/[\s\S]{1,1024}/g) || [];
        } catch (e) {
            var parts = answer;
        }

        if (answer == "") {
            continue;
        }
        for (var j = 0; j < parts.length; j++) {
            if (j == 0) {
                items.push({
                    "name": question,
                    "value": parts[j],
                    "inline": false
                });
            } else {
                items.push({
                    "name": question.concat(" (cont.)"),
                    "value": parts[j],
                    "inline": false
                });
            }
        }
    }

    var options = {
        "method": "post",
        "headers": {
            "Content-Type": "application/json",
        },
        "payload": JSON.stringify({
            "content": "‌", // This is not an empty string
            "embeds": [{
                "title": "Оружейник",
                "fields": items,
                "footer": {
                  "text": "Подпись: Отдел HRB"
                }
            }]
        })
    };

    UrlFetchApp.fetch(POST_URL, options);
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
t800zippygod, 2020-08-08
@t800zippygod

Still transparent, in line 7 in latestResponse you have undefined. And in the line above, you put the value of the last element from the
Variant array into this variable - see what comes into the allResponses array (nothing, it is most likely empty)
Well, then spin the tick further. The error is either in the library you are using, or in how you are using it, or in the request to the server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question