Uncaught TypeError: marker.setMap is not a function

Created on 13 January 2022, over 3 years ago
Updated 18 September 2024, 7 months ago

Problem/Motivation

If you add a new method to Array.prototype then a map can stop working with the message

Uncaught TypeError: marker.setMap is not a function

Steps to reproduce

  1. Set up a site with a geofield and google maps.
  2. Add the following JS to run on the same page
    Array.prototype.customFn = () => 42;
    

Proposed resolution

Don't use jQuery.extend() to make shallow clones of arrays.

Remaining tasks

Review

Original report

Hi everyone,

For the past few weeks, I have a problem with the Google map. It no longer displays and I have the following error :
Uncaught (in promise) TypeError: marker.setMap is not a function
I asked Google who told me to reconfigure my API keys following this process :
1) Create 2 API keys, the first being restricted via HTML Referrers and the second via IP Address.
2) In the Geolocation Google Maps Settings page the key that is HTTP restricted goes in the Google Maps API key field, and the key that is IP restricted goes into the Google Maps API Server key field.

Unfortunately, this does not change anything and I still have the error. However, I haven't touched anything for several months on this project.
Do you know where the problem could be coming from ?
It's a Drupal 8 project and I'm working with the latest version of the module.

Thanks everyone!

🐛 Bug report
Status

Fixed

Version

3.0

Component

Javascript General / Frontend

Created by

🇫🇷France chris44160

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom AndyF

    Very specific situation, but I just had this occur. It looks like in my case it was because a dependency update caused an addition to Array.prototype which polluted the results of shallowCopy in removeMapMarkers() in geolocation-api.js. This is down to using jquery.extend():

    properties inherited from the object's prototype will be copied over

    That's in contrast to Object.assign() which might make more sense to use.

    To clarify what I'm saying open up dev tools on a page with jQuery loaded and run:

    
    jQuery.extend({}, []);
    Array.prototype.customFn = () => 42;
    jQuery.extend({}, []);
    // The result of this second one won't be empty.
    
    

    I've generally heard it's bad practice to modify a built-in prototype, but by the same token should we program defensively and use Object.extend()? (Which also means less jQuery, yay!)

  • 🇬🇧United Kingdom AndyF
  • Merge request !66Prefer Object.assign() to $.extend() → (Merged) created by AndyF
  • Pipeline finished with Success
    8 months ago
    Total: 352s
    #272792
  • Pipeline finished with Success
    8 months ago
    Total: 332s
    #273348
  • Status changed to Needs review 8 months ago
  • 🇬🇧United Kingdom AndyF

    The suggestion in the last comment seemed to fix things for us. But coming back to it the next morning without an issue on production to fix, and actually I wonder why jQuery.extend() was ever being used... it seems to me it's just used to shallow clone an array, whereas I thought jQuery.extend() was all about pulling properties from objects. (But it can work because array elements are properties of objects, but semantically it's not what we want I don't think.)

    I've updated it to just use normal array functions, I think it's a little easier to follow and is still resilient to modification of Array.prototype. Thanks!

  • Pipeline finished with Success
    8 months ago
    Total: 329s
    #273367
  • Status changed to Fixed 8 months ago
  • 🇩🇪Germany christianadamski Berlin, Germany

    Thanks!

    Not an issue in v4 in general - no jQuery there.

  • 🇬🇧United Kingdom AndyF

    Lovely, thanks @christianadamski.

    Hmmn, , TIL...

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024