Reputation: 1583
I deployed the cloud function to Firebase and i wanna get the current server timestamp, but it return {".sv":"timestamp"}} to me.
i have check the post but seems like i using the right code: How do I get the server timestamp in Cloud Functions for Firebase?
My code:
var firebase = require('firebase-admin');
var serviceAccount = require('./privateKey/key.json');
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
databaseURL: 'https://food-ninja-mobile.firebaseio.com/'
});
db = firebase.database();
db.ref(collection).once('value', snap => {
var snapVal = snap.val()
var myData = Object.keys(snapVal).map(key => {
return snapVal[key];
})
var snapLength = myData.length
returnMsg = {}
returnMsg['DbCollection'] = collection
returnMsg['count'] = snapLength
returnMsg['action'] = 'getUserCollectionCount'
returnMsg['timeStamp'] = firebase.database.ServerValue.TIMESTAMP
console.log(returnMsg)
return res.status(200).send(returnMsg)
})
My API: https://us-central1-food-ninja-mobile.cloudfunctions.net/api/getUserCollectionCount?collection=users
Please advise why it will return {".sv":"timestamp"}} instead of timestamp.
Upvotes: 3
Views: 1596
Reputation: 317928
firebase.database.ServerValue.TIMESTAMP
isn't a value that you can return from a Cloud Function. The only way you can use it is as token that you use when inserting data into Realtime Database. That token gets translated by the database server as the current time.
If you want to return the current time as recorded by Google, return the value of Date.now()
.
Upvotes: 6