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
648 stars 109 forks source link

creating document with date field using servertime #127

Closed rmrbytes closed 3 years ago

rmrbytes commented 3 years ago

I am updating documents from Google Sheets to my Firestore database

Minimal Code to Reproduce the Problem

...
const firestore = FirestoreApp.getFirestore(email, key, projectId);
...
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
var data = sheet.getDataRange().getValues();
...
... 
for (i = 1; i < data.length; i++) {
  var field1 = data[i][0];
  var field2 = data[i][1];
  ..
  const json = {
        'f1' : field1,
        'f2': field2,
        'createdOn': FieldValue.serverTimestamp() // this gives an error
      }
      firestore.createDocument('collection/', json);
}
...

Explain the Problem in Detail

Is it possible to set a field using the Server Time Stamp?

Library Version:

33

Thanks

LaughDonor commented 3 years ago

What is FieldValue a reference to, a NodeJS SDK implementation? Is there something wrong with 'createdOn': new Date()?

rmrbytes commented 3 years ago

Thanks for the response. The thought was to use server time. Hence the use of serverTimeStamp in place of new Date(). I am using it in the manner you have suggested.