firebase typeerror:ref.transaction不是一个函数



我对firebase数据库函数有问题。该文档包含以下最小示例,可以使用交易数据:

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)

为什么数据库参考不允许我编写交易数据?我要做的就是总结所有分数和距离。

functions.database.ref不是您想要的,这是听众的建造者。您需要使用event.data.refadmin.database().ref

,但您的细节错误。也许这会帮助其他人:

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

是admin。 database() .ref('')

最新更新