Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot decode type from Firestore Value

I've deployed a Cloud Firebase Function to update some aggregate data but I'm getting

aggregateReceivedRatings: Error: Cannot decode type from Firestore Value: {"integerValue":"3"} at DocumentSnapshot._decodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:464:15) at DocumentSnapshot.get (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:372:17) at exports.aggregateReceivedRatings.functions.firestore.document.onWrite.event (/user_code/lib/index.js:9:32) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) at /var/tmp/worker/worker.js:695:26 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

The function is very similar to the one illustrated on the Firestore solution section for aggregation queries:

exports.aggregateReceivedRatings = functions.firestore.document('users/{userId}/feedbacks_received/{ratingId}')
.onWrite(event => {
  var ratingVal = event.data.get('rating');

  const db = admin.firestore();  
  var restRef = db.collection('users').document(event.params.userId);

  return db.transaction(transaction => {
    return transaction.get(restRef).then(restDoc => {
      var newNumRatings = restDoc.data('received_feedbacks').tot + 1;

      var newSum = restDoc.data('received_feedbacks').sum + ratingVal;

      return transaction.update(restRef, {
        sum: newSum,
        tot: newNumRatings
      });
    });
  });
});

And the rating value is the integer 3.

I've also run

npm install firebase-functions@latest firebase-admin@latest --save

and re-deployed but without luck.

My package.json contains the following:

    {
  "name": "functions",
  "scripts": {
    "build": "./node_modules/.bin/tslint -p tslint.json && ./node_modules/.bin/tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "~5.4.2",
    "firebase-functions": "^0.7.1"
  },
  "devDependencies": {
    "tslint": "^5.8.0",
    "typescript": "^2.5.3"
  },
  "private": true
}

Any help?

like image 203
Alberto Di Gioacchino Avatar asked Oct 28 '22 21:10

Alberto Di Gioacchino


1 Answers

Just update firebase admin to 5.5.1. It have worked for me. Just using the command line in: npm install --save firebase-admin@^5.5.1

like image 76
Dang Hoang Avatar answered Nov 08 '22 04:11

Dang Hoang