TypeError: Cannot read property 'FieldValue' of undefined

Viewed 2721
  19 | event.preventDefault();
  20 | db.collection('todos').add({
  21 |   todo:input,
  22 |   timestamp: firebase.firestore.FieldValue.serverTimestamp()
  23 |  });
  24 | 
  25 | setInput("");

Whenever I try to run this code, it shows me type error. So that I cant able to push the data to firebase

5 Answers

Make sure to import firebase from 'firebase' and not import firebase from './firebase'.

Had the same problem than you, fixed it by doing this.

Make sure you have correct timestamp value, or don't pass timestamps if you don't need them. Try passing this json to todos collection:

{
  todo:input,
  timestamp: firebase.firestore.FieldValue.serverTimestamp() || null
}

Make sure you have FieldValue inside firebase.firestore by logging console.log(firebase.firestore) and try console.log(Object.keys(firebase.firestore))

event.preventDefault();
db.collection('todos').add({
  todo:input,
  timestamp: firebase.Xc.firebase_.firestore.FieldValue.serverTimestamp()
}); 

setInput("");

You can try:

import { serverTimestamp } from '@firebase/firestore'

timestamp: serverTimestamp()

I hope it'll work

Related