Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!