【问题标题】:Cloud Function in Firebase Realtime DatabaseFirebase 实时数据库中的云函数
【发布时间】:2017-11-18 12:46:31
【问题描述】:

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

下面是我一直在做的云功能:

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" : [
       {"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"
}

【问题讨论】:

  • 现在你在/Player 上触发,所以user.uid 上没有概念。 Player 节点是什么样的?
  • @FrankvanPuffelen 嘿。我已经编辑了问题以显示 Player 节点。关于您的评论,我现在看到 Player 节点中没有 user.uid 的概念,所以我将如何访问我打算访问的数据库参考:admin.database().ref(/users/{user.uid}/week1);
  • 我很难理解您的用例并因此提供帮助。也许其他人足够了解它可以提供帮助。

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


【解决方案1】:

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

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

  1. 您的触发器参考不正确。您的数据库节点是“玩家”而不是“玩家”
  2. 您的 var ref 也不正确。您是否尝试从代码中没有的变量插入用户 ID?在设置值之前,您需要知道用户的 ID。

编辑:我建议将用户 uid 添加到播放器?然后在函数执行时,您可以通过执行类似以下操作来访问用户 uid:

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

很难说你在追求什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 2021-10-25
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多