簡體   English   中英

Firebase實時數據庫中的雲功能

[英]Cloud Function in Firebase Realtime Database

我的目標是要有一個databse觸發器,以便當"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節點如下:

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

您的函數有幾個問題,我不會為您重寫函數,但會指出一些事情。

首先,建議您查看以下文檔: 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'];

很難說出你要做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM