Roggie
Roggie

Reputation: 1217

My Firebase Cloud Function fails with Object.value is not a function error?

Why am I getting the following error when I try to fetch the values of my Firebase child nodes under registrationTokens:

Database structure:

"fcmtokens" : {
    "dBQdpR7l1WT2utKVxdX2" : {
      "registrationTokens" : {
        "O": ""c4PSCAUAg5s:Yw95DyVxwElE88LwX7" 
      }
    }
  }

Console output:

TypeError: Object.values is not a function

Part of my deployed function to Firebase Cloud Functions:

return admin.database().ref('/fcmtokens/' + toId + '/registrationTokens').once('value').then((userTok) => {

    const registrationTokens = Object.values(userTok.val());

    console.log('registrationTokens', registrationTokens

Upvotes: 1

Views: 458

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317552

Object.values() is a feature new in ECMAScript 2017.

Cloud Functions runs node 6 by default, which only supports ECMAScript 2015 (ES6). So, if you're running that code in the default Cloud Functions runtime, you will get that error (because the function doesn't exist).

node 8 supports ECMAScript 2017, and Cloud Functions allows you to deploy to node 8. So, if you really need to use Object.values(), you should deploy to node 8 instead. Or you can use the lodash equivalent.

Upvotes: 5

Related Questions