grahamearley / FirestoreGoogleAppsScript

A Google Apps Script library for accessing Google Cloud Firestore.
http://grahamearley.website/blog/2017/10/18/firestore-in-google-apps-script.html
MIT License
647 stars 109 forks source link

Saving timestamp to firestore #86

Closed sephina closed 4 years ago

sephina commented 4 years ago

Hey that a really good library, helped me a lot. Thanks!

I'm trying to create a document with a timestamp in it and I'm keep getting an empty map on Firestore.

firestore.createDocument(USER_EXPIRATION_COLLECTION, { userId: userId, classId: classId, timestamp: new Date()});

and the output:

{ name: 'projects/.../usersClassesExpiration/...', fields: { userId: 'userId', timestamp: {}, classId: 'classId' }, createTime: Tue May 26 2020 11:11:09 GMT+0300 (Israel Daylight Time), updateTime: Tue May 26 2020 11:11:09 GMT+0300 (Israel Daylight Time) }

sinancan34 commented 4 years ago

https://github.com/grahamearley/FirestoreGoogleAppsScript/issues/75#issuecomment-629597429

themcclure commented 4 years ago

I have tested my code against the latest library release (27) and I still get the blank date issue:

createDocument() returns this: fields: { formSubmitted: { mapValue: {} }, preferredName: { stringValue: 'H6' } ... }

LaughDonor commented 4 years ago

@themcclure, I'm not following... where's the blank date? Can you provide more code in your example?

I have a test case called Test_Create_Document_Bare and only passes the collection path to .createDocument(path), which returns an empty object with the random document name that Google assigned and appropriate Create and Update times.

There's another test case called Test_Create_Document_Data which passes in data, and the output matches the input with the appropriate Create and Update times. This unit test also includes a 'timestamp data' field to test data passed in as new Date() (a Date object).

Both tests are passing.

rielzzapps commented 4 years ago

We run into the same issue not saving Date() object in the document. { "dateStart" : new Date() }

Remains blank.

rielzzapps commented 4 years ago

I noticed 2 things:

function wrapDate_ (date) { return {'timestampValue': date.toISOString()} }

The other and I think main issue I found is using the library as a library, the Date object that is passes is no longer evaluating as instanceof Date:

value instanceof Date //false

so Dates get sent as Mapvalues.

When logging the instance in the script calling FirestoreApp, it evaluates as 'true'. Checked it on V8 and non-V8, but not found out yet.

rielzzapps commented 4 years ago

Bit of ugyl fix:

image

LaughDonor commented 4 years ago

Verified that instanceof fails for Date objects outside of the library.

https://github.com/grahamearley/FirestoreGoogleAppsScript/blob/13365c9156c752d52d10f82a3250cf7865d5434f/Document.ts#L140

This line will be updated to include additional checks. Thanks for bringing this up!