如何通过Firestore获取Firebase云服务器中的服务器时间戳记? [英] How do I get the server timestamp in Cloud Functions for Firebase with Firestore?
问题描述
如何获取服务器时间戳,而不使用实时数据库
,而是使用Firestore?
import *作为firebase-functions的函数
$使用服务器时间戳与Firestore有点不同:
从@ google-cloud / firestore导入{Firestore}
$ b $ const db = new Firestore()
导出let testIfMatch = functions.firestore
.document('users / {userId} / invites / {invitedUid}')
.onCreate(event => {
let invite =< any> event.data.data()
if(invite.accepted == true){
return db.collection('matches')。add({
friends:[userId ,inviteUid],
timestamp:doc.readTime //< ---不完全是实际时间
})
}
<$ p $p>
//获取FieldValue对象
var FieldValu e = require(firebase-admin)。FieldValue;
//创建文档引用
var docRef = db.collection('objects')。doc('some-id');
//使用服务器的值更新时间戳字段
var updateTimestamp = docRef.update({
timestamp:FieldValue.serverTimestamp()
});
如果不行,可以编辑 var FieldValue = require(firebase (firebase-admin)。firestore.FieldValue;
How can we get a server timestamp, without using the realtime database but using instead Firestore ?
import * as functions from 'firebase-functions'
import { Firestore } from '@google-cloud/firestore'
const db = new Firestore()
export let testIfMatch = functions.firestore
.document('users/{userId}/invites/{invitedUid}')
.onCreate(event => {
let invite = <any>event.data.data()
if (invite.accepted == true) {
return db.collection('matches').add({
friends: [userId, invitedUid],
timestamp: doc.readTime // <--- not exactly the actual time
})
}
Use server timestamp with Firestore is little different:
// Get the `FieldValue` object
var FieldValue = require("firebase-admin").FieldValue;
// Create a document reference
var docRef = db.collection('objects').doc('some-id');
// Update the timestamp field with the value from the server
var updateTimestamp = docRef.update({
timestamp: FieldValue.serverTimestamp()
});
if it's not working you can edit the var FieldValue = require("firebase-admin").FieldValue;
with var FieldValue = require("firebase-admin").firestore.FieldValue;
这篇关于如何通过Firestore获取Firebase云服务器中的服务器时间戳记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!