Firebase实时数据库中的云功能

时间:2017-11-18 12:46:31

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

我的目标是拥有一个数据库触发器,这样当"Players"数据库节点发生变化时,在名为set ()的另一个firebase节点上会有一个相应的users函数。

以下是我一直在研究的云功能:

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

// The Firebase Admin SDK to access the Firebase Realtime Database. 
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.update = functions.database.ref('/Player')
    .onWrite(event=>{
      console.log(event.data);
      var ref = admin.database().ref(`/users/{user.uid}/week1`);
      ref.set(10);
      return;

    });

问题是云功能会返回错误:

  

错误:Firebase.child失败:第一个参数是无效路径:" / users / {user.uid} / week1"。路径必须是非空字符串...

有多个用户如下所示,有没有办法可以访问每个用户的用户uid,并在我的云功能中引用它?:

 users: {
   user uid (generated by push) : {
       deviceToken : "tokenID",
       name : "Display Name"
   },
   anotherUserID : {
       deviceToken : "tokenID",
       name : "Display Name"
   }

Players节点如下:

{
"players" : [
       {"name" : "Yasin 'YB' Amusan", 
        "Team" : "Industry",
        "price": 8000000, 
        "position": "forward", 
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png", 
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Hassan 'Hasi' Akinyera",
        "Team" : "Industry", 
        "price": 5000000, 
        "position": "defender",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0,
        "Y" : 0,
        "R" : 0},
       {"name" : "Femi 'Fabio' Awoniyi",
        "Team" : "Industry",
        "price": 9000000,
        "position": "defender",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Deji 'Dej' Awoniyi",
        "Team" : "Industry",
        "price": 7000000,
        "position": "forward",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Koye 'K10' Kekere-Ekun",
        "Team" : "Industry",
        "price": 9000000,
        "position":"midfielder",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Teni 'Teezee' Zacchaeus",
        "Team" : "Industry",
        "price": 6000000, 
        "position":"hybrid",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Bolaji 'Boj' Odojukan",
        "Team" : "Industry",
        "price": 7000000,
        "position":"forward",
        "image":"http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
        "goals": 0, 
        "assists" : 0, 
        "Y" : 0, 
        "R" : 0},
       {"name" : "Ernest",
        "Team" : "Industry",
        "price": 6000000,
        "position":"defender"
}

1 个答案:

答案 0 :(得分:0)

你的功能有几个问题,我不会为你重写这个功能,但会指出一些事情。

首先,我建议您查看这些文档:https://firebase.google.com/docs/functions/get-started

  1. 您的触发器参考不正确。您的数据库节点是“玩家”而不是“玩家”
  2. 您的var ref也不正确。您是否尝试从不在代码中的变量插入用户ID?在设置值之前,您需要知道用户的ID。
  3. 编辑:我建议将用户uid添加到玩家?然后在函数执行时,您可以通过执行与此类似的操作来访问用户uid:

    var player = event.data.val();
    var uid = player['useruid'];
    

    告诉你的事情有点难以理解。

相关问题