Firebase函数从不同路径接收数据和总和

时间:2018-07-20 04:44:18

标签: firebase firebase-realtime-database google-cloud-functions

我是firebase函数的新手。我想实现一个可以对不同路径的值求和的函数。这是Firebase实时数据库结构。

sensor:{
  sensor-1: 1
  sensor-2: 1
  sensor-3: 1
  sensor-4: 1
  sum: 4  
 }

我想要可以将传感器1-4值求和并将其写在“ sum:”上的函数js。我的开始

    exports.sumsensor = functions.database.ref('sensor/{id}').onWrite((change)=>{ 
         //compute code in here
         return admin.database().ref('sensor/sum').set('total');
    });

我该如何实现。请帮助我。

1 个答案:

答案 0 :(得分:0)

您将不得不稍微更改数据库结构:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "TakePledgeCell", for: indexPath) as! PledgeTableViewCell
        .....
        cell.plusBtnAction = { labelText in
           //handle labelText here
        }
}

这是云功能代码:

sensor : {
    sensor-1: 1
    sensor-2: 1
    sensor-3: 1
    sensor-4: 1
}

sensorSum : 4

以下是参考链接:firebase transaction