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

jet*_*dev 4 javascript timestamp firebase reactjs google-cloud-firestore

我正在遵循教程,代码中使用 firebase.firestore.FieldValue.serverTimestamp() 将timestampprop 设置为服务器时间戳。

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

但我收到这个错误。

Module not found: Can't resolve 'firebase' in ...
Run Code Online (Sandbox Code Playgroud)

我想 firebase 有某种更新,我们不再导入它了import firebase from "firebase";?我应该怎么做才能使用servertimestamp()

Fra*_*len 7

有关最新示例,请参阅有关添加服务器端时间戳的文档,其中包含此代码段

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()
});
Run Code Online (Sandbox Code Playgroud)

该文档提供了新的 v9 模块化语法和旧语法的并排示例,您可能会在许多第三方教程中找到这些示例,因此您可以查找如何将代码转换为新语法。升级指南也是一个很好的入门方法。