我正在尝试弄清楚如何向在我的 firestore 数据库中创建的文档添加时间戳。
我已阅读并尝试了 this post 中的每一项建议.
我已阅读该帖子上的文档链接,但我无法理解为记录文档创建日期的时间戳而应该尝试实现的步骤。
在我的 firebase 设置中,我有:
firebase.initializeApp(config);
const database = firebase.database();
const fsDB = firebase.firestore();
const settings = { timestampsInSnapshots: true };
export { firebase, database as default, fsDB, settings };
我真的不确定设置 const 是否必要,因为很多关于这一点的帖子表明它现在是 firebase 中的某种默认设置(我找不到关于这一点的 firebase 文档)。
然后,在我的表单中,我有:
import * as firebase from '../../../firebase';
import { fsDB } from "../../../firebase";
我不确定为什么有必要直接导入这些 firebase 文件,因为它们在应用程序入口点中,但有几篇帖子建议这样做。
class Preregistration extends React.Component {
constructor(props) {
super(props);
this.state = {selectedValue: null};
this.state = {createdAt: firebase.database.serverTimestamp()};
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.createdAt.toLocaleTimeString()}.</h2>
</div>
);
}
}
export default Preregistration;
我也试过:
this.state = {createdAt: firebase.fsDB.serverTimestamp()};
this.state = {createdAt: firebase.firestore.FieldValue.serverTimestamp()};
this.state = {createdAt: firebase.FieldValue.serverTimestamp()};
这些方法都不起作用。必须有一种简单的方法可以为文档添加时间戳,以便我可以监控它们的创建时间。为什么弄清楚如何去做如此困难?
当我尝试时:
this.state = {createdAt: firebase.fsDB.FieldValue.serverTimestamp()};
(注意 - 我的 firestore 保存为 fsDB),
我收到一条错误消息:
TypeError: Cannot read property 'serverTimestamp' of undefined
我想也许(因为我定义我的配置的方式)表达式可能意味着阅读:
this.state = {createdAt: fsDB.FieldValue.serverTimestamp()};
但这会产生相同的错误信息
firebase 支持票建议建议:
createdAt: firebase.firestore.Timestamp.now();
当我尝试这样做时,我收到一条错误消息:
类型错误:无法读取未定义的属性“时间戳”
如果知道如何在 firestore 中使用时间戳会很棒。
firebase 文档说:
var docRef = db.collection('objects').doc('some-id');
// Update the timestamp field with the value from the server
var updateTimestamp = docRef.update({
timestamp: firebase.firestore.FieldValue.serverTimestamp()
});
当我尝试这样做时,我收到一条错误消息:
TypeError: 无法读取未定义的属性“FieldValue”
我已经尝试了每一种设置初始值以及单独尝试使用值设置状态的方法。它们都不起作用。
采纳下面 Murray R 的建议,我尝试了 {} 和 () 中显示的这两条注释行。这些尝试都不起作用(页面不会因其中包含的错误而呈现(无法编译,预期;是错误消息)。我已经洒了;在我能想到的每个地方都放了它们,但无法得到这个工作方法。
handleSubmit = (formState, { resetForm }) => {
// Now, you're getting form state here!
console.log("SUCCESS!! :-)\n\n", formState);
fsDB
.collection("contact")
.add(formState)
// .set createdAt: firebase.firestore.FieldValue.serverTimestamp()
// .add createdAt: firebase.firestore.FieldValue.serverTimestamp()
.then(docRef => {
console.log("docRef>>>", docRef);
this.setState({ selectedValue: null });
resetForm(initialValues);
})
.catch(error => {
console.error("Error adding document: ", error);
});
};
最佳答案
要获取 Firestore 时间戳,firebase.firestore.FieldValue.serverTimestamp()
是正确的调用。这里有一个问题:此方法仅在 DocumentReference .set()
或 .update()
调用(或者,我相信,CollectionReference .add( )
)。例如,在将新文档添加到“联系人”集合时,您可以将 formState 与新属性“createdAt”结合起来,并将其传递给 .add()
方法:
handleFormSubmit(event) {
// Now, you're getting form state here!
console.log("SUCCESS!! :-)\n\n", formState);
fsDB
.collection("contact")
.add({
...formState,
createdAt: firebase.firestore.FieldValue.serverTimestamp()
})
.then(docRef => {
console.log("docRef>>>", docRef);
this.setState({ selectedValue: null });
resetForm(initialValues);
})
.catch(error => {
console.error("Error adding document: ", error);
});
}
关于reactjs - Firebase - firestore createdAt 时间戳 - react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56035174/