執行useFetchAll函式后,將回應值傳遞給stringform函式,報錯
型別錯誤:A.forEach 不是函式
在示例中,我創建了變數 asdf 并將 Logger.log(回應)值復制到其中,stringform 函式作業并回傳一個陣列。為什么當值為(回應)時函式會拋出錯誤?
// 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;
}
uj5u.com熱心網友回復:
這是一個深度為 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)
}
uj5u.com熱心網友回復:
關于你的問題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]], Logger.log(response)of var response = UrlFetchApp.fetchAll(req)shows [[["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(',') '
';
});
});
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(',') '
'), "");
Logger.log(values)
}
參考:
fetchAll(請求)
回傳: HTTPResponse[] — 來自每個輸入請求的 HTTP 回應資料陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/358797.html
標籤:javascript 数组 谷歌应用程序脚本 urlfetch
下一篇:將2個陣列合并為一個有2列的陣列
