0

I tried to add this code in Firebase Cloud Function:

exports.getTime = functions.https.onCall((data,context)=>{
    return Date.now()
})

But I am getting this error upon checking on a browser but I am planning to use this on Android.

{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

Do I miss something?

lejlun
  • 4,140
  • 2
  • 15
  • 31

1 Answers1

1

There are a few ways to do this in a Firebase Cloud Function:

const admin = require('firebase-admin');
// or
// import * as admin from 'firebase-admin';

// Using Cloud Firestore
admin.firestore.FieldValue.serverTimestamp()

// Using Realtime Database
admin.database.ServerValue.TIMESTAMP

Or simply:

Date.now()
Andrew
  • 795
  • 4
  • 18
  • I tried doing this: exports.getServerTime = functions.https.onCall((data, context) => { return admin.firestore.FieldValue.serverTimestamp() }); But it's displaying this error >> Error: Forbidden Your client does not have permission to get URL /getServerTime from this server. – Darkzide Demigod Oct 02 '21 at 13:20
  • Returning Date.now() on android works fine but not on web. – Darkzide Demigod Oct 02 '21 at 13:22
  • Then that is a permissions issue. I suggest reading this [SO post](https://stackoverflow.com/questions/47511677/firebase-cloud-function-your-client-does-not-have-permission-to-get-url-200-fr) – Andrew Oct 04 '21 at 07:03