MapQuest, PlaceSearch.js (in promise) error

59 Views Asked by At

I have a simple input field, in which when the user starts typing it will call the place-search.js script. When I start typing I get the following error:

Uncaught (in promise) Error: Uncaught, unspecified "error" event. ([object Response])

here is the function that listens for user input in the text field.

function addlistener(id){
        // This example will load jQuery dynamically, but this could be any script...
        loadScripts({
          test: function() { return typeof placeSearch !== 'undefined'; },
          scripts: 'https://api.mqcdn.com/sdk/place-search-js/v1.0.0/place-search.js',
          done: function() { 
           placeSearch({
                key: key,
                container: document.querySelector('#'+id),
                useDeviceLocation: useDeviceLocation,
                collection: collection
              });
          }
        }); 
    }

Can someone explain what this error means, or how to go about fixing it? Any guidance or help would be appreciated.

2

There are 2 best solutions below

1
MQBrian On

Is there someplace we can see this running now? Can you confirm the place-search.js file has successfully loaded before it is called?

0
theguywhodoesntstop On

I was able to figure out the problem. So when you look at the function:

function addlistener(id){
        // This example will load jQuery dynamically, but this could be any script...
        loadScripts({
          test: function() { return typeof placeSearch !== 'undefined'; },
          scripts: 'https://api.mqcdn.com/sdk/place-search-js/v1.0.0/place-search.js',
          done: function() { 
           placeSearch({
                key: key,
                container: document.querySelector('#'+id),
                useDeviceLocation: useDeviceLocation,
                collection: collection
              });
          }
        }); 
    }

the useDeviceLocation variable holds the users current location. The app will prompt the user to allow us to get their location. If the user says "no", that variable will remain null. The function requires that variable to have a value thats not null.

So there are many ways to fix this:

Fix 1: Simply remove the useDeviceLocation: useDeviceLocation line.

Fix 2: We can write an if loop, which will check to see if useDeviceLocation is null. If it is, it will run case 1 code, or else it will run case 2 code. In our situation, there is only two cases, either we have a location, or don't.

function addlistener(id,useDeviceLocation){
if(useDeviceLocation){
  loadScripts({
      test: function() { return typeof placeSearch !== 'undefined'; },
      scripts: 'https://api.mqcdn.com/sdk/place-search-js/v1.0.0/place-search.js',
      done: function() { 
       placeSearch({
            key: key,
            container: document.querySelector('#'+id),
            useDeviceLocation: useDeviceLocation,
          });
      }
    }); 
}else{
  loadScripts({
    test: function() { return typeof placeSearch !== 'undefined'; },
    scripts: 'https://api.mqcdn.com/sdk/place-search-js/v1.0.0/place-search.js',
    done: function() { 
      placeSearch({
        key: key,
        container: document.querySelector('#'+id),
      });
    }
  });   
}
}