Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Apps Script Google Direction API - arrival_stop returning ERROR

I am trying to return arrival_stop.name from Google directions API in an Apps Script function. Here is my code:

const GOOGLEMAPS_STEPS_TRANSIT_DETAILS = (origin, destination, mode = 'transit') => {
  const { routes = [] } = Maps.newDirectionFinder()
    .setOrigin(origin)
    .setDestination(destination)
    .setMode(mode)
    .getDirections();
  if (!routes.length) {
    throw new Error('No route found!');
  }

  var stepTransitDetails = routes
    .map(({ legs }) => {
      return legs.map(({ steps }) => {
        return steps.map((step) => {
          return step.transit_details.arrival_stop.name;
        });
      });
    })
    .join(' , ');

  return stepTransitDetails;
};

Error given:

TypeError: Cannot read property 'arrival_stop' of undefined (line 228).

Desired output is a list of stops within the transit step - for example if the step is taking the Piccadilly Line train from Leicester Square to Kings Cross the output would be the list of stations it will stop at.

I have this same code working when returning step.html_instructions rather than step.transit_details.arrival_stop.name and if I remove the .arrival_stop.name it returns [object Object],,object Object], meaning it's recognising the steps which involve transit, but adding the .arrival_stop gives an error.

Working based on Google API Docs

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When I saw the data of routes, it seems that there are objects without including the property of transit_details in step. I thought that this might be the reason for your issue. If you want to retrieve step.transit_details.arrival_stop.name, how about the following modification?

From:

var stepTransitDetails = routes
  .map(({ legs }) => {
    return legs.map(({ steps }) => {
      return steps.map((step) => {
        return step.transit_details.arrival_stop.name;
      });
    });
  })
  .join(' , ');

To:

var stepTransitDetails = routes
  .map(({ legs }) => {
    return legs.map(({ steps }) => {
      return steps.filter(({ transit_details }) => transit_details).map(({ transit_details }) => {
        return transit_details.arrival_stop.name;
      });
    });
  })
  .join(' , ');
  • In this modification, filter is used for retrieving the object including transit_details.

Reference:

  • filter()
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda