如何获取Cloud Functions for Firebase中的服务器时间戳?

时间:2023-01-25 10:10:34

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 there? Use case is me wanting to timestamp an email when it arrives.

我知道你可以在web,ios和android中提取服务器时间戳 - 但是新的Cloud Functions for Firebase呢?我无法弄清楚如何获得服务器时间戳?用例是我希望在邮件到达时为其添加时间戳。

On web it is Firebase.database.ServerValue.TIMESTAMP

在Web上,它是Firebase.database.ServerValue.TIMESTAMP

But that doesn't seem to be available in the functions node server interface?

但是这似乎在功能节点服务器接口中不可用?

I think it is late and I may be missing the point here...

我觉得已经很晚了,我可能会在这里忽略这一点......

EDIT

编辑

I am initializing like this

我正在这样初始化

admin.initializeApp(functions.config().firebase);
const fb = admin.database()

Then, it is being called like this..

然后,它被称为这样..

Firebase.database.ServerValue.TIMESTAMP

But, that is from a client side integration. On Functions, Firebase isn't initialized like this. I've tried

但是,这是来自客户端集成。在功能上,Firebase未像这样初始化。我试过了

admin.database().ServerValue.TIMESTAMP

and

fb.ServerValue.TIMESTAMP

3 个解决方案

#1


52  

Since you're already using the admin SDK, the correct syntax is:

由于您已经在使用admin SDK,因此正确的语法是:

admin.database.ServerValue.TIMESTAMP

#2


17  

If you use the Firebase Admin SDK, here is the correct syntax (Checked 2017-12-07):

如果您使用Firebase Admin SDK,则此处的语法正确(Checked 2017-12-07):

const admin = require('firebase-admin');

// Using Cloud Firestore
admin.firestore.FieldValue.serverTimestamp()

// Using Realtime Database
admin.database.ServerValue.TIMESTAMP

#3


7  

I'm new to node.js myself, but Date.now() works in my tests.

我自己是node.js的新手,但Date.now()在我的测试中起作用。

Edit

编辑

I misunderstood you question--didn't realize you wanted to timestamp data you were storing in the Firebase database. I thought you simply wanted to get the time on the server that was running your cloud function. If you want to timestamp a received email being stored in the Firebase database, then using admin.database.ServerValue.TIMESTAMP is without question the best approach.

我误解了你的问题 - 没有意识到你想要将你存储在Firebase数据库中的数据加时间戳。我以为你只是想在运行你的云功能的服务器上得到时间。如果要为收到的电子邮件存储在Firebase数据库中的时间戳,那么使用admin.database.ServerValue.TIMESTAMP毫无疑问是最好的方法。

Just for my own education, I wrote the following function to see how the times compare. I would expect the times on the cloud function server and database server are synced to a very accurate time reference. When I run this function, the database timestamp typically within a hundred milliseconds of the Date.now() value. The database timestamp being a little later is reasonable, given that it takes the cloud function some time to connect to the database and perform the write.

仅仅为了我自己的教育,我写了以下函数来看看时间比较。我希望云功能服务器和数据库服务器上的时间同步到非常准确的时间参考。当我运行此函数时,数据库时间戳通常在Date.now()值的一百毫秒内。数据库时间戳稍晚是合理的,因为云功能需要一段时间才能连接到数据库并执行写入。

exports.timeTest = functions.database.ref('/test/trigger')
    .onWrite(event => {

        const now= Date.now();
        console.log('now=', now);

        const timeVals = {
          dateNow : now,
          serverTimestamp : admin.database.ServerValue.TIMESTAMP
        };

        return event.data.ref.parent.child('times').update(timeVals);
    });

#1


52  

Since you're already using the admin SDK, the correct syntax is:

由于您已经在使用admin SDK,因此正确的语法是:

admin.database.ServerValue.TIMESTAMP

#2


17  

If you use the Firebase Admin SDK, here is the correct syntax (Checked 2017-12-07):

如果您使用Firebase Admin SDK,则此处的语法正确(Checked 2017-12-07):

const admin = require('firebase-admin');

// Using Cloud Firestore
admin.firestore.FieldValue.serverTimestamp()

// Using Realtime Database
admin.database.ServerValue.TIMESTAMP

#3


7  

I'm new to node.js myself, but Date.now() works in my tests.

我自己是node.js的新手,但Date.now()在我的测试中起作用。

Edit

编辑

I misunderstood you question--didn't realize you wanted to timestamp data you were storing in the Firebase database. I thought you simply wanted to get the time on the server that was running your cloud function. If you want to timestamp a received email being stored in the Firebase database, then using admin.database.ServerValue.TIMESTAMP is without question the best approach.

我误解了你的问题 - 没有意识到你想要将你存储在Firebase数据库中的数据加时间戳。我以为你只是想在运行你的云功能的服务器上得到时间。如果要为收到的电子邮件存储在Firebase数据库中的时间戳,那么使用admin.database.ServerValue.TIMESTAMP毫无疑问是最好的方法。

Just for my own education, I wrote the following function to see how the times compare. I would expect the times on the cloud function server and database server are synced to a very accurate time reference. When I run this function, the database timestamp typically within a hundred milliseconds of the Date.now() value. The database timestamp being a little later is reasonable, given that it takes the cloud function some time to connect to the database and perform the write.

仅仅为了我自己的教育,我写了以下函数来看看时间比较。我希望云功能服务器和数据库服务器上的时间同步到非常准确的时间参考。当我运行此函数时,数据库时间戳通常在Date.now()值的一百毫秒内。数据库时间戳稍晚是合理的,因为云功能需要一段时间才能连接到数据库并执行写入。

exports.timeTest = functions.database.ref('/test/trigger')
    .onWrite(event => {

        const now= Date.now();
        console.log('now=', now);

        const timeVals = {
          dateNow : now,
          serverTimestamp : admin.database.ServerValue.TIMESTAMP
        };

        return event.data.ref.parent.child('times').update(timeVals);
    });