I am facing a challenge with Google Cloud Spanner as I work with multiple tables containing columns of type Date. While inserting entries with specified dates works fine, the issue arises when trying to retrieve these entries in the JavaScript frontend. The problem lies in understanding the format of these date fields.
My Google Cloud Functions code retrieves a customer from the database in the following manner:
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 frontend, I have the following code:
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
appears to have a nested property dateOfBirth that is difficult to decipher due to its structure. How can I convert this property into a string with the format 'yyyy-mm-dd' or any other appropriate date format?
Any guidance or insights on this will be greatly appreciated. Thank you!