【发布时间】:2019-09-09 05:40:15
【问题描述】:
我正在将文档添加到表单的 Firestore 集合 (inboxItems) onSubmit。
onCreateInboxItem = event => {
this.props.firebase.inboxItems().add({
name: this.state.newInboxItemName,
created: '', // I need a timestamp field here
})
this.setState({ name: '' });
event.preventDefault();
}
如何让created 字段成为时间戳字段,并将当前时间戳作为值?它需要在用户和时区之间保持一致。
我看到firebase docs 中提到了firebase.firestore.FieldValue.serverTimestamp(),但此时尚未设置字段值。我想避免更新字段的额外操作。
【问题讨论】:
-
我不清楚为什么不能使用 FieldValue.serverTimestamp()。您只需在调用 add() 时将其用作字段的值。
-
"this.props.firebase.firestore.FieldValue.serverTimestamp()" 似乎不起作用。也许我在这里引用了一些错误的东西,但我认为 FieldValue 只有在字段已经设置时才能访问。
标签: reactjs firebase google-cloud-firestore