【问题标题】:Why does the function fail after UrlFetchApp.fetchAll () is executed?为什么 UrlFetchApp.fetchAll() 执行后函数会失败?
【发布时间】:2021-11-15 21:23:09
【问题描述】:

执行useFetchAll函数后,将响应值传递给stringform函数,报错

TypeError: A.forEach 不是函数

在示例中,我创建了变量 asdf 并将 Logger.log(响应)值复制到其中,stringform 函数工作并返回一个数组。为什么值为(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(',') + '%0A';
        });
      });
      return formad;
      
    }

【问题讨论】:

  • 错误代码是什么?
  • TypeError: A.forEach 不是函数
  • 啊,那么 A 可能不是数组
  • 在您的情况下,此处的 A asdf.forEach(A => { 仍然是一个数组数组,因为您有一个 3 维数组数组。所以你需要把它弄平一点
  • A 不能是数组。应该是一个字符串。你需要JSON.parse(A)它来获取底层数组。

标签: javascript arrays google-apps-script urlfetch


【解决方案1】:

这是一个深度为 3 的数组。

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

你的低级函数本来可以工作,但 asdf 没有在该函数中定义。

你的下层函数是这样运行的:

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)
      
    }

【讨论】:

    【解决方案2】:

    关于你对Why does the function fail after UrlFetchApp.fetchAll () is executed?的问题,从你的脚本和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)?,我认为你的问题的原因是因为fetchAll返回HTTPResponse[]这是一个数组。

    例如,当请求req 之一返回像[["a",1],["a",2],["a",3],["a",4],["a",5]] 这样的二维数组时,var response = UrlFetchApp.fetchAll(req)Logger.log(response) 显示[[["a",1],["a",2],["a",3]], [["a",1],["a",2],["a",3]]]。在您的情况下,我认为来自多个请求的值可能包含在 response 中。

    如果我对你的情况的理解是正确的,那么下面的修改呢?

    修改脚本:

    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(',') + '%0A';
        });
      });
      return formad;
    }
    

    或者,

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

    参考:

    • fetchAll(requests)

      返回:HTTPResponse[] — 来自每个输入请求的 HTTP 响应数据数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-01
      • 2013-03-30
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2016-04-11
      相关资源
      最近更新 更多