sangeeth kumar
sangeeth kumar

Reputation: 21

TypeError: Cannot read property 'FieldValue' of undefined

  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

Upvotes: 2

Views: 5409

Answers (6)

ATOM
ATOM

Reputation: 31

This solution works for me:

const {Timestamp} = require("firebase-admin/firestore")

{
  todo:input,
  timestamp: Timestamp.now(),
}

Upvotes: 0

Lucas David Ferrero
Lucas David Ferrero

Reputation: 1902

For those who are using the Admin SDK with NodeJS:

import { FieldValue } from '@google-cloud/firestore'

const docData = {
  userName: 'Lucas'
  createdAt: FieldValue.serverTimestamp()
}

You could find all the external API's references here: https://firebase.google.com/docs/reference/admin/node/firebase-admin.firestore

Upvotes: 7

Kendi
Kendi

Reputation: 11

You can try:

import { serverTimestamp } from '@firebase/firestore'

timestamp: serverTimestamp()

I hope it'll work

Upvotes: 1

Ahmet Can
Ahmet Can

Reputation: 1

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

setInput("");

Upvotes: 0

lomr69
lomr69

Reputation: 21

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

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

Upvotes: 2

user14080657
user14080657

Reputation: 128

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))

Upvotes: 1

Related Questions