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

132
Visualizações
How to work with Google Spanner Date type in Javascript?

I have multiple tables in Google Cloud Spanner with columns of type Date. Inserting entries with specified dates is not a problem, and reflects appropriately in the database. However, retrieving entries from the database into the Javascript frontend is where the issue lies. I can't seem to figure out in which format these date fields are.

My (Google Cloud Functions) code retrieves a customer from the database as follows:

async function fetch(database, query) {
  query.json = true;
  try {
    const [rows] = await database.run(query);
    return rows;
  } catch (error) {
    console.error("ERROR:", error);
    throw error;
  } finally {
    database.close();
  }
}

exports.fetchCustomer = functions.https.onCall(async (data, context) => {
  const database = instance.database(data.dataset);
  let address = fetch(database, {
    sql: `SELECT * FROM addresses WHERE addressId = '${data.id}'`,
  });
  let customer = fetch(database, {
    sql: `SELECT * FROM customers WHERE customerId = '${data.id}'`,
  });

  try {
    [address, customer] =
      await Promise.all([address, customer]);
    return [address[0], customer[0]];
  } catch (error) {
    console.error("ERROR:", error);
    throw error;
  }
});

On the front-end side, I have:

const fetchCustomer = httpsCallable(firebaseFunctions, 'fetchCustomer');
await fetchCustomer({dataset: this.dataset, id: this.customerId})
.then((res) => {
  this.address = res.data[0];
  this.customer = res.data[1];
}

The object this.customer then has a property dateOfBirth which seems to be an infinitely nested object, which I can't make sense of. How can I convert that property to a String with the format 'yyyy-mm-dd' or any other appropriate date format?

Any advice or information on the matter would be appreciated. Thanks in advance!

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

0

This sample has an example (https://cloud.google.com/spanner/docs/samples/spanner-query-with-date-parameter#spanner_query_with_date_parameter-nodejs):

  rows.forEach(row => {
    const date = row[2]['value'];
    const json = row.toJSON();
    console.log(
      `VenueId: ${json.VenueId}, VenueName: ${json.VenueName},` +
        ` LastContactDate: ${JSON.stringify(date).substring(1, 11)}`
    );   }); 
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