javascript - 无法从 Firestore 值解码类型

标签 javascript firebase google-cloud-functions google-cloud-firestore

我已经部署了 Cloud Firebase Function 来更新一些聚合数据,但我得到了

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)

该功能与聚合查询的 Firestore 解决方案部分中说明的功能非常相似:

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
      });
    });
  });
});

评分值为整数3。

我也跑过

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

并重新部署但没有运气。

我的 package.json 包含以下内容:

    {
  "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
}

有什么帮助吗?

最佳答案

只需将 firebase admin 更新到 5.5.1。它对我有用。只需在命令行中使用: npm install --save firebase-admin@^5.5.1

关于javascript - 无法从 Firestore 值解码类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47886043/

相关文章:

node.js - 如何使用 VSCode 通过 Firestore 触发器调试 Google Cloud Functions?

javascript - OOP - 访问对象以更改其变量?

hash - Firebase 简单登录的盐/哈希?

javascript - javascript中的动态索引设置

firebase - 添加数据读取限制 firebase firestore

java - 使用 Android 处理 Firebase 引用的最佳方式

python-3.x - 如何在 Google Cloud Functions 上部署基本的 python 函数(带包)

javascript - 存储来自不同 Intent 的多个参数

JavaScript- 在页面中查找文本并跳转到页面中的位置

javascript - 我如何从 View 页面中的下面的 javascript 代码调用 Controller 函数(Codeigniter)