【发布时间】:2018-08-15 16:24:36
【问题描述】:
我尝试实现以下目标:当计数更改为“2”时,我需要该函数将名为“updates”的 JSON 推送到数据库中的特定位置,并从 PlayerQueue 节点 (0:"Mik ", 1:"Bg" 等) 并将其作为 "id" 放入数据库。所以问题是我需要它获取前两个节点(在这种情况下为 0 和 1)并从中取出名称(Mik 和 Bg)并将它们作为 id1 和 id2 放入数据库中(在这个数据库中我只有一个id 值,但我稍后会添加它),问题是我无法弄清楚如何从前两个节点中取出名称。
我的数据库:
这是我的代码
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { resolve } from 'url';
//Game/queue/{queueId}/PlayerCount
admin.initializeApp(functions.config().firebase);
exports.createGame = functions.database.ref('Game/queue/PlayerCount').onUpdate((change, context) => {
const ref1 = admin.database().ref('/Game/queue/PlayerQueue').limitToFirst(1);
var tmp:String = 'esh';
ref1.once("value")
.then(result => {
tmp = result.val();
console.log(tmp)
var updates = {};
updates['id'] = tmp
updates['visible'] = {
place: 'a1',
sign: 'rock'
};
const after = change.after.val();
if(after.count == 2){
return admin.database().ref('/Game/allGames').push(updates);
}
return null
}).catch(reason => {
console.log(reason)
});
return null;
});
【问题讨论】:
标签: javascript firebase firebase-realtime-database google-cloud-functions