【发布时间】:2015-09-27 05:15:03
【问题描述】:
我正在尝试创建用于检索 XML 数据的缓存。
我的问题是我收到一个错误“TypeError: Cannot find function forEach in object”
我不明白为什么我会收到错误,我从一个示例中复制了大部分内容。
错误发生在:
priceIDs.forEach (function (row) {
row.forEach ( function (cell) {
if (typeof(cell) === 'number' ) {
dirtyTypeIDs.push(cell);
}
});
});
“row.forEach(函数(单元格){”行似乎是罪魁祸首。我将在下面发布整个代码以供人们帮助:)
function LoadPrices(priceIDs, systemID,cacheBuster){
if (typeof systemID == "undefined") {
systemID=30000142;
}
if (typeof priceIDs == "undefined") {
throw "need typeids";
}
if (typeof cacheBuster == "undefined") {
cacheBuster=1;
}
var prices = new Array();
var dirtyTypeIDs = new Array();
var cleanTypeIDs = new Array();
var url = "http://api.eve-central.com/api/marketstat?cacheBuster="+cacheBuster+"&usesystem="+systemID+"&typeid=";
priceIDs.forEach (function (row) {
row.forEach ( function (cell) {
if (typeof(cell) === 'number' ) {
dirtyTypeIDs.push(cell);
}
});
});
cleanTypeIDs = dirtyTypeIDs.filter(function(v,i,a) {
return a.indexOf(v)===i;
});
var parameters = {method : "get",payload: ""};
var xmlFeed = UrlFetchApp.fetch(url+cleanTypeIDs.join("&typeid="),parameters).getContent();
var xml = XmlService.parse(xmlFeed);
if(xml)
{
var rows=xml.getRootElement().getChild("marketstat").getChildren("type");
for(var i = 0; i< rows.length; i++) {
var price = [rows[i].getAttribute("id").getValue(),
rows[i].getChild("sell").getChild("volume").getValue(),
rows[i].getChild("sell").getChild("avg").getValue(),
rows[i].getChild("sell").getChild("max").getValue(),
rows[i].getChild("sell").getChild("min").getValue(),
rows[i].getChild("sell").getChild("stddev").getValue(),
rows[i].getChild("sell").getChild("median").getValue(),
rows[i].getChild("sell").getChild("percentile").getValue(),
rows[i].getChild("buy").getChild("min").getValue(),
rows[i].getChild("buy").getChild("avg").getValue(),
rows[i].getChild("buy").getChild("max").getValue(),
rows[i].getChild("buy").getChild("min").getValue(),
rows[i].getChild("buy").getChild("stddev").getValue(),
rows[i].getChild("buy").getChild("median").getValue(),
rows[i].getChild("buy").getChild("percentile").getValue(),
];
prices.push(price);
}
};
}
谁能帮我理解错误并纠正它?
【问题讨论】:
-
确保
row是一个数组,forEach()是一个数组方法。 -
你能显示
priceIDs吗? -
您确定这是您使用 forEach 的数组元素吗?浏览器可能不支持 forEach。你可能需要一个 polyfill:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript object foreach typeerror