简体   繁体   English

Firebase实时数据库中的云功能

[英]Cloud Function in Firebase Realtime Database

I aim to have a databse trigger such that when there is a change in the "Players" databse node, there will be a corresponding set () function at a different firebase node called users . 我的目标是要有一个databse触发器,以便当"Players"数据库的节点发生更改时,在名为users的另一个Firebase节点上将有一个对应的set ()函数。

Below is the cloud function I have been working on: 以下是我一直在从事的云功能:

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;

    });

The issue is that the cloud function returns the error: 问题是云函数返回错误:

Error: Firebase.child failed: First argument was an invalid path: "/users/{user.uid}/week1". 错误:Firebase.child失败:第一个参数是无效路径:“ / users / {user.uid} / week1”。 Paths must be non-empty strings... 路径必须是非空字符串...

There are multiple users as follows so is there a way I can access what the user uid will be for each user and refer to this within me cloud function?: 有多个用户,如下所示,有没有办法我可以访问每个用户的用户uid并在我的云函数中引用它?:

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

The Players node is as follows : 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"
}

There are a couple issues with your function, I will not rewrite the function for you, but will point out some things. 您的函数有几个问题,我不会为您重写函数,但会指出一些事情。

First, I suggest taking a look at these docs: https://firebase.google.com/docs/functions/get-started 首先,建议您查看以下文档: https : //firebase.google.com/docs/functions/get-started

  1. Your trigger reference is not correct. 您的触发器参考不正确。 Your database node is "players" not "Player" 您的数据库节点是“玩家”而不是“玩家”
  2. Your var ref is not correct either. 您的var ref也不正确。 Are you trying to insert the user id from a variable that is not in your code? 您是否要从代码中未包含的变量中插入用户ID? You will need to know the user's id before you can set values. 在设置值之前,您将需要知道用户的ID。

Edit: I suggest adding the user uid to the players ? 编辑:我建议将用户uid添加到播放器中? Then on function execution you can access the user uid by doing something similar to this: 然后在执行函数时,您可以通过执行以下操作来访问用户uid:

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

It is a little hard to tell what you are after. 很难说出你要做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果Firebase实时数据库中存在数据库节点,请使用云功能检查 - Checking with Cloud Function if a database node exists in Firebase Realtime Database Firebase 实时数据库中的云函数 - Cloud Functions in Firebase Realtime Database 使用 Firebase Cloud Function 增加新用户的实时数据库计数 - Increment Realtime Database count on new user with Firebase Cloud Function 如何从云功能中止到Firebase实时数据库的创建? - How to abort creation into firebase realtime database from a cloud function? 使用 javascript 将 firebase 云 function 中的数组发送到实时数据库 - Send an array in firebase cloud function to the realtime database using javascript 如何使用云功能将子级添加到Firebase实时数据库的列表中? - How to add child to a List in Firebase Realtime Database with a cloud function? Firebase HTTP触发云功能可更改实时数据库中的用户值 - Firebase HTTP Trigger Cloud Function to change users' values in realtime database 如何使用云 function 获取 Firebase 中的实时数据库? - How to get Realtime Database in Firebase by using cloud function? 如何使用 Javascript 云从 Firebase 实时数据库中随机获取子密钥名称 function - How to get a child key name randomly from the Firebase Realtime database with a Javascript cloud function 如何做云 function firebase 将数据从 Bucket 实时导入数据库 - how to do a cloud function firebase importing data from Bucket to database realtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM