我在当前项目中使用Firebase Firestore数据库,并且必须从前端反应添加文档,为此我需要当前时间戳。
import * as firebase from "firebase";
import React, {useState} from 'react';
const [time, setTime] = useState({''}) //here i need current timestamp from firebase.
答案 0 :(得分:1)
那么您需要从Firebase导入Firestore, 不要使用firebase.firestore.FieldValue.serverTimeStamp,它将无法正常工作
这是工作示例
import {firestore} from "firebase";
const timestamp = firestore.FieldValue.serverTimestamp();
答案 1 :(得分:0)
firebase.initializeApp(firebaseConfig);
const projectFirestore = firebase.firestore();
const timestamp = firebase.firestore.FieldValue.serverTimestamp;
答案 2 :(得分:0)
firebase.initializeApp(firebaseConfig); const timestamp = firebase.firestore.FieldValue.serverTimestamp;
还请注意,尽管@shubham Takode答案是正确的,但在客户端不会填充Firebase Firestore当前时间戳,因此尝试在react useState()管理中使用它不会有任何好处。
import * as firebase from "firebase";
import React, {useState} from 'react';
firebase.firestore
.collection('user')
.doc('userid1')
.set({
username: "first user",
email: "user@email.com",
time: firebase.firestore.FieldValue.serverTimestamp() // This sets a placeholder in your record that get authomatically populated with the serverTime once it gets to your firestore db.
}).then(/*do somethings on success*/).catch(err => {/*do somethings on err*/})