Reputation: 21
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
Reputation: 31
This solution works for me:
const {Timestamp} = require("firebase-admin/firestore")
{
todo:input,
timestamp: Timestamp.now(),
}
Upvotes: 0
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
Reputation: 11
You can try:
import { serverTimestamp } from '@firebase/firestore'
timestamp: serverTimestamp()
I hope it'll work
Upvotes: 1
Reputation: 1
event.preventDefault();
db.collection('todos').add({
todo:input,
timestamp: firebase.Xc.firebase_.firestore.FieldValue.serverTimestamp()
});
setInput("");
Upvotes: 0
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
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