le0
le0

Reputation: 851

Is there any way to use FieldValue.serverTimestamp() in Firebase Firestore JS without Firebase-Admin

According to answer below, Firebase-Admin is required to use FieldValue.serverTimestamp().

How do I get the server timestamp in Cloud Functions for Firebase with Firestore? .

But I am using only Firebase JS and Firebase Firestore JS. The application runs only in client side. However, Firebase-Admin requires a server.

Is there any way to use server timestamp when doing a write operation ?

Upvotes: 11

Views: 16553

Answers (4)

Hemant Bhatia
Hemant Bhatia

Reputation: 21

try to import firebase like this -

import firebase from "firebase/compat/app";

Upvotes: 0

Big_Boulard
Big_Boulard

Reputation: 1335

https://firebase.google.com/docs/reference/js/firestore_.md#servertimestamp

import { serverTimestamp } from "firebase/firestore";

addDoc(collection(db, 'your_collection'), {
  ...
  createdAt: serverTimestamp()
})

Upvotes: 9

Orlandster
Orlandster

Reputation: 4858

This answer is outdated, check above answer!

Simply use Firebase.ServerValue.TIMESTAMP within your object.

Like this:

ref.set({ time: Firebase.ServerValue.TIMESTAMP })

Upvotes: -4

Ahmed Raza
Ahmed Raza

Reputation: 429

you can get the instance from your firebase instance:

firebase.firestore.FieldValue.serverTimestamp();

Upvotes: 3

Related Questions