5
votes

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?

1
Well I don't have a solution for this problem, but what I can tell you is that you are not alone on this one.´Today I wanted to execute a cloud function, that worked just fine yesterday, but today I got the same error as you did and neither redeploying or changing the code solved that problem. I have a couple of functions and all work just fine, except the ones with a Database Trigger. Reading and writing within a http trigger function executes without any error. So I figure this has nothing to do with our code,but rather with Firebase/Firestore itself.Max
Hey Alberto. Firebaser here. It's not immediately clear what would cause this, but our engineers suspect it might be related to your dependencies. Can you edit your question to include your package.json?Frank van Puffelen
Edited by adding package.json contentAlberto Di Gioacchino

1 Answers

2
votes

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