Node js Google Routes Library Field Mask

248 Views Asked by At

I'm trying to use Node Js Google Library the method compute Routes. But in every execution i try i have: "catch getPathFromGoogle Error: 3 INVALID_ARGUMENT: FieldMask is a required parameter. See https://cloud.google.com/apis/docs/system-parameters on how to provide it. As an example, you can set the header 'X-Goog-FieldMask' to value 'routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline' to ask for the route distance, duration, and polyline in the response. You can also set the value to '' in manual testing to get all the available response fields. However, using the '' wildcard is discouraged in production.'" How can i send this info to this method?

Here is my code:

                r = await routingClient.computeRoutes({
                    params: {
                        origin: { lat: origin[1], lng: origin[0] },
                        destination: {lat: dest[1], lng: dest[0]}
                      },
                      timeout: 1000
                });

An example of a code working with this method and how put X-Goog-FieldMask would be perfect.

1

There are 1 best solutions below

0
Maxim Mazurok On

Here's a working example:

import routing from "@googlemaps/routing";
import { GoogleAuth } from "google-auth-library";

const routingClient = new routing.v2.RoutesClient({
  authClient: new GoogleAuth().fromAPIKey(
    "YOUR_API_KEY"
  ),
});

async function callComputeRoutes() {
  // Run request
  const response = await routingClient.computeRoutes(
    {
      origin: {
        address: "Zetland, Sydney, NSW, Australia",
      },
      destination: {
        address: "Coogee Beach, Sydney, NSW, Australia",
      },
    },
    {
      otherArgs: {
        headers: {
          "X-Goog-FieldMask": "*",
        },
      },
    }
  );
  console.log(response);
}

callComputeRoutes();

I couldn't find this in the docs, couldn't figure this out from the library sources or comments, had to just try and got lucky :)