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!
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)}`
); });