【问题标题】:Firebase TypeError: ref.transaction is not a functionFirebase TypeError:ref.transaction 不是函数
【发布时间】:2017-08-04 02:42:21
【问题描述】:

我对 firebase 数据库功能有疑问。 documentation contains the following minimal example 用于处理事务数据:

var upvotesRef = db.ref("server/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes");
upvotesRef.transaction(function (current_value) {
  return (current_value || 0) + 1;
});

现在,我的 firebase 数据库结构如下所示

/game-results
    /RaUALJvTZVea5y6h1lzqGJPMpuI3
        /distance
        /score
        /timestamp
    /3ItAqKHL0XRUOum2Ajdz9hHQxMs1
        /distance
        /score
        /timestamp
/statistics
    /total-distance
    /total-score

我想总结一下分数和距离。我正在使用以下函数,这里为total-distance

const functions = require('firebase-functions');

exports.updateTotalDistance = functions.database.ref('/game-results/{userId}/distance')
    .onWrite(event => {
        const newDistance = event.data.val();
        const distanceRef = functions.database.ref('/statistics/total-distance')
        return distanceRef.transaction(function(currentDistance) {
        console.log('currentDistance', currentDistance);
            return (currentDistance || 0) + newDistance;
        });
    });

但是,我无法理解为什么在 firebase 函数日志中出现以下错误:

TypeError: distanceRef.transaction is not a function
    at exports.updateTotalDistance.functions.database.ref.onWrite.event (/user_code/index.js:19:22)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)

为什么数据库引用不允许我写入事务数据?我要做的就是总结所有分数和距离。

【问题讨论】:

    标签: node.js firebase reference transactions typeerror


    【解决方案1】:

    functions.database.ref 不是你要找的,那是听众的建造者。你会想要使用event.data.refadmin.database().ref

    【讨论】:

      【解决方案2】:

      但是你有一个细节错误。也许这会帮助其他人:

      它不是管理员。数据库.ref('')

      它是管理员。database().ref('')

      【讨论】:

      • functions.database().ref('/accounts') 或 admin.database().ref('/accounts') 不适合我
      猜你喜欢
      • 2020-07-01
      • 2020-03-14
      • 2019-06-07
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 2017-10-07
      相关资源
      最近更新 更多