Home > OS >  Why does the function fail after UrlFetchApp.fetchAll () is executed?
Why does the function fail after UrlFetchApp.fetchAll () is executed?

Time:11-16

After executing the useFetchAll function, I pass the response value to the stringform function, and I get an error

TypeError: A.forEach is not a function

In the example, I made the variable asdf and copied the Logger.log (response) value into it, the stringform function works and returns an array. Why does the function throw an error when the value is (response)?

    // Using UrlFetchApp.fetchAll()
    function useFetchAll(req) {
    var response = UrlFetchApp.fetchAll(req);
     
    var asdf=[[["a",1],["a",2],["a",3],["a",4],["a",5]], [["b",6],["b",7],["b",8],["b",9],["b",10]], [["c",11],["c",12],["c",13],["c",14],["c",15]], [["d",16],["d",17],["d",18],["d",19],["d",20]], [["e",21],["e",22],["e",23],["e",24],["e",25]]];
     
    Logger.log(response);
    Logger.log(stringform(asdf));
     
    }
     
    
   function stringform(asdf) {
    let formad = '';
      asdf.forEach(A => {
        A.forEach(r => {
          formad  = r.join(',')   '
';
        });
      });
      return formad;
      
    }

CodePudding user response:

It's an array that's 3 deep.

function myfunction() {
  let s = '';
  var asdf = [[["a", 1], ["a", 2], ["a", 3], ["a", 4], ["a", 5]], [["b", 6], ["b", 7], ["b", 8], ["b", 9], ["b", 10]], [["c", 11], ["c", 12], ["c", 13], ["c", 14], ["c", 15]], [["d", 16], ["d", 17], ["d", 18], ["d", 19], ["d", 20]], [["e", 21], ["e", 22], ["e", 23], ["e", 24], ["e", 25]]];
  asdf.forEach(A => {
    A.forEach(r => {
      s  = r.join(',')  '\n';
    })
  })
  Logger.log(s);
}

Execution log
3:53:29 PM  Notice  Execution started
3:53:29 PM  Info    a,1
a,2
a,3
a,4
a,5
b,6
b,7
b,8
b,9
b,10
c,11
c,12
c,13
c,14
c,15
d,16
d,17
d,18
d,19
d,20
e,21
e,22
e,23
e,24
e,25
3:53:30 PM  Notice  Execution completed

You lower function would have worked but asdf was not defined in that function.

Your lower function runs like this:

function stringform(asdf) {
  var asdf = [[["a", 1], ["a", 2], ["a", 3], ["a", 4], ["a", 5]], [["b", 6], ["b", 7], ["b", 8], ["b", 9], ["b", 10]], [["c", 11], ["c", 12], ["c", 13], ["c", 14], ["c", 15]], [["d", 16], ["d", 17], ["d", 18], ["d", 19], ["d", 20]], [["e", 21], ["e", 22], ["e", 23], ["e", 24], ["e", 25]]];
    let formad = '';
      asdf.forEach(A => {
        A.forEach(r => {
          formad  = r.join(',')   '\n';
        });
      });
      Logger.log(formad)
      
    }

CodePudding user response:

About your question of Why does the function fail after UrlFetchApp.fetchAll () is executed?, from your script and In the example, I made the variable asdf and copied the Logger.log (response) value into it, the stringform function works and returns an array. Why does the function throw an error when the value is (response)?, I thought that the reason of your issue is due to that fetchAll returns HTTPResponse[] which is an array.

For example, when one of requests req returns 2 dimensional array like [["a",1],["a",2],["a",3],["a",4],["a",5]], Logger.log(response) of var response = UrlFetchApp.fetchAll(req) shows [[["a",1],["a",2],["a",3]], [["a",1],["a",2],["a",3]]]. In your situation, I thought that the values from the muliple requests might be included in response.

If my understanding of your situation is correct, how about the following modification?

Modified script:

function useFetchAll(req) {
  var response = UrlFetchApp.fetchAll(req);
  var asdf = response.map(r => JSON.parse(r.getContentText()));
  Logger.log(stringform(asdf));
}

function stringform(asdf) {
  let formad = '';
  asdf.forEach(A => {
    A.forEach(r => {
      formad  = r.join(',')   '
';
    });
  });
  return formad;
}

or,

function useFetchAll(req) {
  var response = UrlFetchApp.fetchAll(req);
  var values = response.reduce((t, r) => t  = JSON.parse(r.getContentText()).map(c => c.join(',')   '
'), "");
  Logger.log(values)
}

Reference:

  • fetchAll(requests)

    Return: HTTPResponse[] — An array of HTTP response data from each input request.

  • Related