Home > Net >  Uncaught Error: Cannot find module 'firebase' when trying to use serverTimestamp in firest
Uncaught Error: Cannot find module 'firebase' when trying to use serverTimestamp in firest

Time:09-28

when I try to add serverTimestamp() i get the error that firestore (web V9) is not imported. is there any way to use serverTimestamp or just import firestore/firebase so that I can use it?


addDoc(collection(db, 'rooms', roomId, 'messages'), {
  message:input,
  name:user.displayName,
  timestamp:firebase.firestore.FieldValue.serverTimestamp()// the issue 
})

CodePudding user response:

There is no change in serverTimestamp, all you need is to import firebase&firestore correctly (version 9 compat)

import firestore from 'firebase/compat';
import firebase from 'firebase/compat/app';

CodePudding user response:

You don't need the admin.firestore.FieldValue namespace to use serverTimestamp() in the new Modular SDK. You can simply import it as shown below:

import { addDoc, collection, serverTimestamp } from "firebase/firestore"
// import serverTimestamp ----->^

addDoc(collection(db, 'rooms', roomId, 'messages'), {
  message: input,
  name: user.displayName,
  timestamp: serverTimestamp() // <-- usage 
})

Importing compat version of Firestore just for serverTimestamp() while you are actually using Modular SDK kind of defeats purpose of having Modular SDK.

From the documentation,

When using both compat and Modular version, your app will compile, you won't get the app size benefits of the modular code until you fully remove the compat statements and code from your app.

  • Related