Omar
Omar

Reputation: 3040

Object doesn't support this property or method 'entries' (polyfill not working in edge)

I have the same issue as this Object doesn't support property or method 'entries' and I installed the polyfill as one of the first scripts to run on my app.

This is the JS that has error

const formdata = new FormData(form);
for (var value of formdata.entries()) {
   console.log(value); 
}

Here Is the polyfill I am trying

if (!Object.entries) { 
    //this does not run on Edge. So Object.entries must exist
    Object.entries = function( obj ){
        var ownProps = Object.keys( obj ),
            i = ownProps.length,
            resArray = new Array(i); // preallocate the Array
        while (i--)
          resArray[i] = [ownProps[i], obj[ownProps[i]]];

        return resArray;
    };
}

The script as it is above does not run on Edge. So Object.entries must exist. So I tried the following.

Object.entries = function( obj ){
    var ownProps = Object.keys( obj ),
        i = ownProps.length,
        resArray = new Array(i); // preallocate the Array
    while (i--)
      resArray[i] = [ownProps[i], obj[ownProps[i]]];

    return resArray;
};

Now I know for sure that the script is on the page. but when I use Object.entries the error comes before the polyfill was used

How come the error runs instead of the polyfill on edge despite it definitely being on the page?

Upvotes: 1

Views: 2007

Answers (1)

apsillers
apsillers

Reputation: 115940

This Q&A you linked to, which told you to use an Object.entries polyfill to supply an entries method on FormData instances, is absolutely incorrect. The polyfill you've supplied for Object.entries adds an entries method to the Object constructor object: that is, you may safely use the literal expression Object.entries(something) to call the method entries on the object Object. Object.entries is a different function and does a different operation from the entries method that exists on FormData instances.

Instead, you need a polyfill for Formdata.prototype.entries. One option is available at https://github.com/github/form-data-entries, though it achieves its goal by having you write formDataEntries(myForm) instead of new FormData(myform).entries(). This is not a proper polyfill (they call it a "ponyfill"), since it does not add an entries method to FormData but instead supplies an entirely new function that does the same thing.

Upvotes: 2

Related Questions