How to access firestore.Timestamp from Firebase Cloud Function

后端 未结 5 1784
遥遥无期
遥遥无期 2021-02-12 17:46

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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-12 18:36

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

    });

提交回复
热议问题