如何使用 Firestore 在 Cloud Functions for Firebase 中获取服务器时间戳? [英] How do I get the server timestamp in Cloud Functions for Firebase with Firestore?
本文介绍了如何使用 Firestore 在 Cloud Functions for Firebase 中获取服务器时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不使用实时数据库的情况下获取服务器时间戳而是使用 Firestore ?
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
})
}
推荐答案
使用服务器时间戳与 Firestore 有点不同:
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()
});
如果它不起作用,您可以使用 var FieldValue = require("firebase-admin").firestore 编辑
var FieldValue = require("firebase-admin").FieldValue;
.字段值;
if it's not working you can edit the var FieldValue = require("firebase-admin").FieldValue;
with var FieldValue = require("firebase-admin").firestore.FieldValue;
这篇关于如何使用 Firestore 在 Cloud Functions for Firebase 中获取服务器时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文