Firebase TypeError:ref.transaction不是函数

Afr*_*Afr 6 transactions reference typeerror node.js firebase

我有firebase数据库函数的问题.该文档包含以下用于处理事务数据的最小示例:

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

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

/game-results
    /RaUALJvTZVea5y6h1lzqGJPMpuI3
        /distance
        /score
        /timestamp
    /3ItAqKHL0XRUOum2Ajdz9hHQxMs1
        /distance
        /score
        /timestamp
/statistics
    /total-distance
    /total-score
Run Code Online (Sandbox Code Playgroud)

我想总结得分和距离.我正在使用以下功能,这里用于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;
        });
    });
Run Code Online (Sandbox Code Playgroud)

但是,我无法解决为什么我在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)
Run Code Online (Sandbox Code Playgroud)

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

Jam*_*els 11

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


Luc*_*ski 5

但您的细节有误。也许这会对其他人有所帮助:

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

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