Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue getting an unambiguous error message from Node.js Client for Google Maps Services #952

Open
CyrilKamal opened this issue Mar 6, 2023 · 2 comments
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@CyrilKamal
Copy link

CyrilKamal commented Mar 6, 2023

Environment details

  1. Using the Directions API
  2. Windows 11 Version 22H2
  3. @googlemaps/[email protected]

Steps to reproduce

I am trying to use the Google Maps Directions API in my Node.js application to get optimized routes between multiple waypoints. I haven't been able to make successful requests and receive responses using the API, and the problem is I can't debug the problem because I am getting an error that I can't get to be specific.

my client object looks like this:

client
  .directions({
    params: {
      origin: origin, //{placeId:"ChIJdd4hrwug2EcRmSrV3Vo6llI"},
      destination: destination, //{placeId: "ChIJh1a5WhEMa0gRY1JU4PEam8Q"},
      waypoints: waypts,
      optimize: true,
      mode: 'driving',
      key: process.env.GOOGLE_MAPS_API_KEY
    },
    timeout: 1000, // milliseconds
  })
  .then((r) => {
    console.log("got r")
    const routeOrder = r.data.routes[0].waypoint_order;
    for(let i = 0; i < reqList.length; i++){
      reqList[i] = reqList[i].replaceAll(' ', '+');
    }
    const n = reqList.length
    reorder(reqList, routeOrder, n)
    console.log(reqList)
  })
  .catch((e) => {
    console.log('Directions request failed due to ' + e);
  });


});

and origin, destination and waypts looks like this

"50 Madeleine St, Kitchener, ON N2R 1V4, Canada"
"Lola, 4th Avenue, Seattle, WA, USA"
[
  { location: '"New York, NY, USA"', stopover: true },
  { location: '"Vancouver, BC"', stopover: true },
  { location: '"Port Angeles, WA, USA"', stopover: true }
]

I've shown my whole client object with the try-catch; I suspect I don't know how to properly catch the Error because what I get is (Directions request failed due to Error).

At the GitHub for the NodeJS client for Maps API: https://github.com/googlemaps/google-maps-services-js/blob/master/README.md You can see in the quick start they want to log the error message like this e.response.data.error_message but when I try to do it that way I get console.log('Directions request failed due to ' + e.response.data.error_message);TypeError: Cannot read properties of undefined (reading 'data')

So I think there is a bug in how to get an actual helpful error code. I also tried looking into the props of the error object and went down e.config.data as that seemed the closest, but e.config.data returns undefined and has no further properties like an error_message prop.

Stack trace

console.log('Directions request failed due to ' + e.data.error_message);
                                                             ^

TypeError: Cannot read properties of undefined (reading 'error_message')
@CyrilKamal CyrilKamal added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Mar 6, 2023
@wangela
Copy link
Member

wangela commented Mar 6, 2023

If you would like to upvote the priority of this issue, please comment below or react with 👍 so we can see what is popular when we triage.

@CyrilKamal Thank you for opening this issue. 🙏
Please check out these other resources that might help you get to a resolution in the meantime:

This is an automated message, feel free to ignore.

@CyrilKamal
Copy link
Author

CyrilKamal commented Mar 7, 2023

Kind of a workaround, but if you go to the deprecated google maps node client located here, it works perfectly fine and returns an actual error code. Here is the working code with the deprecated client

googleMapsClient.directions({origin: origin, destination: destination, waypoints:waypts, optimize:true, mode: "driving"})
    .asPromise()
    .then((response) => {
    console.log(response)
    const routeOrder = response.json.routes[0].waypoint_order;
    for(let i = 0; i < reqList.length; i++){
      reqList[i] = reqList[i].replaceAll(' ', '+');
    }
    const n = reqList.length
    reorder(reqList, routeOrder, n)
    console.log(reqList)
    })
    .catch((err) => {
    console.log(err.json.status);
    });

I just wanted to put this here if it happens to help you guys with debugging :)

@usefulthink usefulthink added priority: p2 Moderately-important priority. Fix may not be included in next release. and removed triage me I really want to be triaged. labels Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants