1

I am writing a telemetry function in my code. I am using the Performance.getEntriesByType() function mentioned at the MDN link here - https://developer.mozilla.org/en-US/docs/Web/API/Performance/getEntriesByType. As per the link it mentions that the method returns a list of PerformanceEntry objects / empty list. However, when I invoke the "forEach" method on the returned value of the window.performance.getEntriesByType(type) method, I get the following error

TypeError: Object [object Object] has no method 'forEach'

Couple of doubts:

  1. Why do I get the error 'forEach' has no method?
  2. What does Object [object Object] in the error message imply?
1
  • 1
    Please edit your question to include your actual code. What browser are you using? Commented Feb 1, 2017 at 18:09

2 Answers 2

3

It means that you are attempting to invoke the forEach() method on an object that does not have forEach() method. forEach() is implemented on Array.prototype (meaning that all arrays have this method) and some browsers implement it on DOM nodeList objects. But, you are either trying to use it on another kind of object or in a browser that doesn't support it.

performance.getEntriesByType() returns a list, which is not the same thing as an Array, which is where forEach() is implemented.

The page you referenced, shows how you can loop through the resulting list from the window.performance.getEntriesByType() call.

  // Use getEntries() to iterate through the each entry
  var p = performance.getEntries();
  for (var i=0; i < p.length; i++) {
    log("Entry[" + i + "]");
    check_PerformanceEntry(p[i]);
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Any help on why I see the error as Object [object Object]? Why the 3 object identifiers?
@Saint The default conversion of an object to a string is "[object Object]", so really you are just seeing the type of object you have an error with ("Object") and then the string representation of that, which shows it's underlying type as well ("[object Object]").
1

I personally ran into a bug on an android device and the issue was in a dependency's code so I couldn't change it. I used this polyfill to fix the issue:

if (window.NodeList && !NodeList.prototype.forEach) {
    NodeList.prototype.forEach = function (callback, thisArg) {
        thisArg = thisArg || window;
        for (var i = 0; i < this.length; i++) {
            callback.call(thisArg, this[i], i, this);
        }
    };
}

1 Comment

Thanks @jlafay this polyfill made my Ionic 5 app work on my prehistoric Android 4.4 phone. I just had to change the way of checking if NodeList exists: if (window.hasOwnProperty('NodeList') && !NodeList.prototype.forEach)

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.