简体   繁体   中英

Firebase Cloud Function database.ServerValue.TIMESTAMP not returning timestamp

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM