最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use firebase.firestore.FieldValue.serverTimestamp() now? - Stack Overflow

programmeradmin5浏览0评论

I am following a tutorial and firebase.firestore.FieldValue.serverTimestamp() is used in the code to set timestamp prop as the server 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());
  };

But I get this error.

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

I suppose firebase had some kind of update that we don't import it with import firebase from "firebase"; anymore? What should I do to use the servertimestamp()?

I am following a tutorial and firebase.firestore.FieldValue.serverTimestamp() is used in the code to set timestamp prop as the server 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());
  };

But I get this error.

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

I suppose firebase had some kind of update that we don't import it with import firebase from "firebase"; anymore? What should I do to use the servertimestamp()?

Share Improve this question asked Oct 25, 2021 at 1:37 jethro-devjethro-dev 3391 gold badge7 silver badges14 bronze badges 4
  • "I am following a tutorial" Can you provide a link please, so that we can check what assumptions the tutorial makes? – Frank van Puffelen Commented Oct 25, 2021 at 1:41
  • For the actual error message, this has been ing up quite regularly recently since Firebase change the syntax of its JavaScript SDKs in v9. For example, see stackoverflow./questions/69044315/… – Frank van Puffelen Commented Oct 25, 2021 at 1:43
  • youtube./watch?v=pUxrDcITyjg at 3:07:42. Thanks. – jethro-dev Commented Oct 25, 2021 at 1:44
  • Given when this tutorial came out, it must be using version 8 or earlier of the SDKJs, which had a different syntax. You will either have to import the same version of the SDK as the tutorial used, or upgrade all code and imports as shown in firebase.google./docs/web/modular-upgrade or in the link I gave earlier. – Frank van Puffelen Commented Oct 25, 2021 at 1:45
Add a ment  | 

1 Answer 1

Reset to default 7

For an up to date sample, see the documentation on adding a server-side timestamp, which contains this snippet

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()
});

The documentation has side-by-side examples for both the new v9 modular syntax and the older syntax that you're likely to find in many third-party tutorials, so you can look up how to translate the code to the newer syntax. The upgrade guide is a good way to get started with that too.

发布评论

评论列表(0)

  1. 暂无评论