Home > Mobile >  firebase timestamp obejct recives a _ before every key when added using a cloud funciton?
firebase timestamp obejct recives a _ before every key when added using a cloud funciton?

Time:05-16

Does the default behavior of a cloud function is to add stringyfy values of in someway ? my time stamp results when getting the value using this following function adds a _ before keys in objects ?

await auth.currentUser ?
  .getIdTokenResult()
  .then((token: any) => {
    // {_seconds : num, _nanoseconds : num}
    console.log(token ? .claims.premiumUntill)

  })
  .catch((error: any) => {
    console.log(error);
  });

after checking the docs the time stamp object looks like following ?

// Default
{seconds :  number ,  nanoseconds :  number}

// Recived 
{_seconds :  number ,  _nanoseconds :  number}

Cloud function model

admin
  .auth()
  .getUserByEmail(context.auth ? .token ? .email!)
  .then((user) => admin.auth().setCustomUserClaims(user.uid, {
    premiumUntill: admin.firestore.Timestamp.fromDate(createdAt)
  })),

CodePudding user response:

Does the default behavior of a cloud function is to add stringyfy values of in some way?

Yes, but it's not a behavior of the Cloud Function environment. It's a function of Firebase Auth custom claims. If you give it an object (such as a Timestamp), it will call toJSON on it to get a JSON-compatible representation of the object which is suitable for storage in custom claims (which can store JSON only). That's what you're seeing. You probably shouldn't depend on this, and instead convert it to something more predictable, like milliseconds since epoch.

  • Related