【问题标题】:Firebase issue, I need to take out values from first two nodesFirebase 问题,我需要从前两个节点中取出值
【发布时间】: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


    【解决方案1】:

    由于您要查找队列中的前两个子节点,因此您应该按它们的 ID 排序,然后限制获得 2 个子节点:

    const query = admin.database().ref('/Game/queue/PlayerQueue').orderByKey().limitToFirst(2);
    

    然后就可以监听值了:

    query.once("value").then(snapshot => {
      snapshot.forEach(child => {
        console.log(child.key+": "+child.val());
      });
    });
    

    上面纯粹解决了“获取前两个子节点”的问题。

    更新:要将两个孩子放入单独的变量中,您可以执行以下操作:

    query.once("value").then(snapshot => {
      var first, second;
      snapshot.forEach(child => {
        console.log(child.key+": "+child.val());
        if (!first) {
          first = child.val();
        }
        else if (!second) {
          second = child.val();
        }
      });
      if (first && second) {
        // TODO: do something with first and second
      }
    });
    

    【讨论】:

    • 嗯,事情就是这样,那些节点的 id-s 将是完全随机的,那么它会起作用吗??
    • 你能告诉我如何分别取这两个值吗?
    • 我不确定我理解你所说的“单独”是什么意思。如果您要问如何将两者提取到单独的变量中,我会遍历子项并将它们分配在那里。
    • 是的,这正是我想要的,我只是不知道如何在 forEach 循环中做到这一点,你能展示一下吗??
    • 我添加了一个例子。确保在复制/粘贴之前完全理解它,因为这是一个相当基本的编程结构。
    【解决方案2】:

    除非您有正当理由使用实时数据库,否则请使用 Firestore。

    请参阅 Cloud Firestore 触发器文档,了解如何采取行动 .onUpdateqty 2。请参阅下面的documentation example

    exports.updateUser = functions.firestore
        .document('users/{userId}')
        .onUpdate((change, context) => {
          // Get an object representing the document
          // e.g. {'name': 'Marie', 'age': 66}
          const newValue = change.after.data();
    
          // ...or the previous value before this update
          const previousValue = change.before.data();
    
          // access a particular field as you would any JS property
          const name = newValue.name;
    
          // perform desired operations ...
        });
    

    【讨论】:

    • 我有正当理由使用实时数据库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2019-10-09
    相关资源
    最近更新 更多