2017-03-13 201 views
2

我有一个Firebase数据库函数的问题。该documentation contains the following minimal example与交易数据的工作:Firebase类型错误:ref.transaction不是函数

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

现在,我的火力点的数据库结构如下所示

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

不过,我不能换我周围为什么我得到的火力功能记录以下错误头:

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) 

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

回答

2

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

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

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

相关问题