How do I get the server timestamp in Cloud Functions for Firebase?

前端 未结 7 1226
轮回少年
轮回少年 2020-12-03 00:12

I know you can pull the server timestamp in web, ios, and android - but what about the new Cloud Functions for Firebase? I can\'t figure out how to get the server timestamp

7条回答
  •  忘掉有多难
    2020-12-03 00:52

    It's in the documentation August 16, 2020.

    https://cloud.google.com/firestore/docs/manage-data/add-data#server_timestamp

    // Get the `FieldValue` object
    const FieldValue = admin.firestore.FieldValue;
    
    // Create a document reference
    const docRef = db.collection('objects').doc('some-id');
    
    // Update the timestamp field with the value from the server
    const res = await docRef.update({
      timestamp: FieldValue.serverTimestamp()
    });
    

    Here's how I used it in my code:

    const createUserDoc = (user) => {
        const FieldValue = admin.firestore.FieldValue
        return db.collection('users').doc(user.uid).set({
            memberSince: FieldValue.serverTimestamp(),
        })
    }
    

提交回复
热议问题