【发布时间】: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