gpt4 book ai didi

javascript - 现在如何使用 firebase.firestore.FieldValue.serverTimestamp()?

转载 作者:行者123 更新时间:2023-12-05 01:27:10 27 4
gpt4 key购买 nike

我正在学习教程,代码中使用 firebase.firestore.FieldValue.serverTimestamp() 将 timestamp 属性设置为服务器时间戳。

import firebase from "firebase";
const someFunction = () => {
addDoc(collection(db, `rooms/${roomId}/messages`), {
message: input,
name: user.displayName,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
}).then((res) => getMessagesData());
};

但是我得到了这个错误。

Module not found: Can't resolve 'firebase' in ...

我想 firebase 有某种更新,我们不再使用 import firebase from "firebase"; 导入它了吗?我应该如何使用 servertimestamp()

最佳答案

有关最新示例,请参阅 adding a server-side timestamp 上的文档,其中包含此代码段

import { updateDoc, serverTimestamp } from "firebase/firestore";

const docRef = doc(db, 'objects', 'some-id');

// Update the timestamp field with the value from the server
const updateTimestamp = await updateDoc(docRef, {
timestamp: serverTimestamp()
});

该文档提供了新的 v9 模块化语法和您可能会在许多第三方教程中找到的旧语法的并排示例,因此您可以查看如何将代码转换为较新的语法句法。 upgrade guide也是开始的好方法。

关于javascript - 现在如何使用 firebase.firestore.FieldValue.serverTimestamp()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69702035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com