1

I'm trying to create a cache for retrieving XML data.

My problem is im getting an error "TypeError: Cannot find function forEach in object"

I dont understand why im getting the error and i copied most of this out from an example.

The error occurs at:

 priceIDs.forEach (function (row) {
 row.forEach ( function (cell) {
  if (typeof(cell) === 'number' ) {
    dirtyTypeIDs.push(cell);
  }
 });
 });

The line "row.forEach ( function (cell) {" appears to be the culprit. I will post the whole of the code below for people to help :)

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

Can any help me understand the error and correct it?

3
  • Ensure that row is an array, forEach() is an Array method. Commented Jul 9, 2015 at 2:39
  • Can you show priceIDs? Commented Jul 9, 2015 at 2:39
  • Are you sure that this is an array element you are using the forEach on? The browser may not support forEach. You may need a polyfill: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jul 9, 2015 at 2:40

2 Answers 2

1

Short answer:

Your row variable is actually an Object, so you can't use forEach. You can use this code to iterate row properly:

for (var column in row) {
    if (row.hasOwnProperty(column)) {
        var cell = row[column];

        if (typeof cell == "number") {
            dirtyTypeIDs.push(cell);
        }
    }
}

More information here.


Long answer:

A TypeError is something that happens when you attempt to access a property of an Object, but the property is missing.

In the first case, you ask priceIDs (an Array) to call its forEach function. It does this happily, giving you the result you need (row).

However, in the second case, you ask row (an Object) to call its forEach function. row doesn't know anything called forEach, so it dies immediately.

To iterate properties of an Object, you need to use a for...in loop. The code above will take row, iterate the appropriate properties, and push their values to dirtyTypeIDs based on the logic you provided. The question linked below the code answers why the hasOwnProperty check is necessary.

Sign up to request clarification or add additional context in comments.

Comments

0

the function "forEach" is the function for Array object. you can also find the browser compatibility with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.