0

Few weeks back map.put used to work, but now we get this error message intermittently :

var mapTypeSubtype = new Map();
Uncaught TypeError: mapTypeSubtype.put is not a function
    at Test.js:411
    at Array.forEach (<anonymous>)
    at objGenericListener (Test.js:410)

Is this an issue from Javascript api side? I saw the documentation and there is no method like .put , it has been changed to .set. But the strange thing is put used to work without any issues, but now its intermittently working. Any suggestions? To change it to .set it will be a big change for us as we have used maps in many places in our project.

Update

After doing a lot of search from where the put method is introduced, I saw that there is a js which gets loaded in browser which is internal to salesforce. And this was defined in that.

Map.prototype.put = function(a, b) {
    a && "undefined" != typeof b && (a in this.map || this.size++,
    this.map[a] = b)
}

So i think this might not be loaded sometimes which is breaking our implementation.

Thanks

Nagendra Singh
  • 577
  • 1
  • 7
  • 24
  • 3
    There was never a `.put` method in `Map.prototype`. Did you have a custom `Map.prototype.put` before? – adiga Aug 28 '19 at 12:36
  • No, we always had this declared : `var mapTypeSubtype = new Map();` – Nagendra Singh Aug 28 '19 at 12:39
  • How was that possibly working? This post was asked in 2010, back in then also there was no put method! https://stackoverflow.com/questions/2966947/dynamically-add-data-to-a-javascript-map – Jayraj Aug 28 '19 at 12:56
  • Map never had put. Check here https://www.ecma-international.org/ecma-262/6.0/#sec-map-constructor. Official documentation. You sure no body added custom put in Map prototype? – Mahendra Pratap Aug 28 '19 at 13:03
  • Yes I am sure, even now it works sometimes. – Nagendra Singh Aug 28 '19 at 13:44
  • Do this: `console.log(String(Map.prototype.put));` If that ever prints something besides `undefined`, take that string and search your codebase to find where it's implemented, and _delete it_. Then use `set()` instead. FYI, refactoring all occurrences of `.put()` in a codebase should be very simple with a sufficiently advanced IDE such as Notepad++, Sublime Text, VSCode, etc. – Patrick Roberts Aug 28 '19 at 21:49

1 Answers1

0

As you knew that there is not any put method in the Map class, as the @Mahendra Pratap mentioned on ES6 document (https://www.ecma-international.org/ecma-262/6.0/#sec-map-constructor).

You can run something like below script before using Map to prevent refactoring your project:

Map.prototype.put = function(key, value){
    return this.set(key, value);
}

More details

Some Guesses of Why your code working:

  • Maybe there is Map.prototype.put somewhere on your project or in the included libraries.
  • There is put method in the HashMap class in the java not in the javascript. Make sure you are using javascript.
  • You can find some implementation of HashMap in javascript here: JavaScript Hashmap Equivalent
Mehdi Yeganeh
  • 2,019
  • 2
  • 24
  • 42