How to access firestore.Timestamp from Firebase Cloud Function

后端 未结 5 1569
再見小時候
再見小時候 2021-02-12 17:38

We are in the middle of converting our Firestore Date object to the new Timestamp Objects

We have done so successfully on the front end by importing firestore



        
相关标签:
5条回答
  • 2021-02-12 18:23

    At this very moment in time, Timestamp is simply not available in the latest version of the @google-cloud/firestore npm module for node. You can also see that it's not included in the API docs. Perhaps it will be added in the next release of @google-cloud/firestore.

    0 讨论(0)
  • 2021-02-12 18:24

    This worked for me from node:

    firebase = require('firebase')
    firebase.firestore.Timestamp
    
    0 讨论(0)
  • 2021-02-12 18:27

    Since you need it inside a cloud function, then you are working with triggers (onCreate, onUpdate...etc) which carries the timestamp via snap parameter. you can use it as below. (it worked with me)

    exports.newUserCreated = functions.firestore.document('users/{userId}').onCreate(async (snap, context) => {
    firestore.collection(`users/${userID}/lists`).add({
        'created_time': snap.updateTime,
        'name':'new list name',
    }).then(documentReference => {
        console.log("list created");
        return null;
      }).catch(error => {
        console.error('Error creating list', error);
        process.exit(1);
    });
    

    });

    0 讨论(0)
  • 2021-02-12 18:38

    I came across the same problem and so created a basic node module for Firebase Firestore Timestamps here https://www.npmjs.com/package/firebase-firestore-timestamp

    Basic code port from https://www.gstatic.com/firebasejs/4.13.0/firebase-firestore.js

    0 讨论(0)
  • 2021-02-12 18:40

    With the release of V2.0 of Firebase Functions is looks like they've added Timestamp support in the Firebase Admin SDK package.

    Have a look at the official docs here.

    import { firestore } from 'firebase-admin';
    ...
    const now = firestore.Timestamp.now()
    
    0 讨论(0)
提交回复
热议问题