如何使用Firebase Cloud功能在DataSnapshot中查找特定值 [英] How to look for a specific value in a DataSnapshot with Firebase Cloud Functions

查看:73
本文介绍了如何使用Firebase Cloud功能在DataSnapshot中查找特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个将在HTTP请求(在计时器上发送)上触发的云函数,该函数将删除具有特定值的所有子项.

I'm trying to make a cloud function that will trigger on HTTP request (which is sent on a timer), that will remove all childs with a specific value.

数据库节点如下所示:

activities
    4GI1QXUJG0MeQ8Bq19WOdCQFo9r1 //uid
        activity: "hammer"
        id: some ID
        note: "some note"
        timestamp: some timeintervalsince1970
    7IDUiufhiws8939hdfIUHiuhwdi5
        etc....

我想浏览所有活动,如果活动值为"hammer",我想删除该孩子.

I want to look through all the activities, and if the activity value is "hammer", I want to remove the child.

这是我到目前为止所拥有的

This is what I have so far

exports.deleteHammerNotifications = functions.https.onRequest((req, res) => {

    admin.database().ref('activities').once('value', (snapshot) => {

        console.log(snapshot.val())

    });
});

打印:

{ 
    '4GI1QXUJG0MeQ8Bq19WOdCQFo9r1': 
      { activity: 'nn',
        id: '4GI1QXUJG0MeQ8Bq19WOdCQFo9r1',
        note: 'Blank note...',
        timestamp: 1498032472 },
     M6xQU5XWTEVbSqBnR3HBAEhA9hI3: 
      { activity: 'hammer',
      id: 'M6xQU5XWTEVbSqBnR3HBAEhA9hI3',
      note: 'some note here...',
      timestamp: 1497973839 },
}

我的问题是我不知道如何循环遍历DataSnapshot并查找具有以下活动的所有子项:"hammer"值. 我已经在数组的xcode项目中完成了类似的功能,但是我不知道如何使用JavaScript.

My problem is I don't know how to cycle through the DataSnapshot and look for all the childs that has the activity: "hammer" value. I have done similar function in my xcode project with arrays, but I don't know how to do it with JavaScript.

感谢您的帮助!

推荐答案

要在匹配的子节点之间循环,请使用snapshot.forEach():

To cycle through the matching child nodes, use snapshot.forEach():

exports.deleteHammerNotifications = functions.https.onRequest((req, res) => {
    admin.database().ref('activities').once('value', (snapshot) => {
      snapshot.forEach((childSnapshot) => {
        console.log(childSnapshot.val())
      });
    });
});

但是您仍然缺少查询以选择正确的节点.如果没有这样的查询,您可能会调用admin.database().ref('activities').remove().

But you're still missing a query here to select the correct nodes. Without such a query you might as well call admin.database().ref('activities').remove().

要最有效地从数据库中删除多个项目并将单个响应写回用户,请使用此功能(我从最近需要的功能中修改了此功能):

To most efficiently delete a number of items from the database and write a single response back to the user, use this function (which I modified from something I needed recently):

exports.cleanup = functions.https.onRequest((req, res) => {
  var query = admin.database().ref("activities").orderByChild("activity").equalTo("hammer");
  query.once("value").then((snapshot) => {
    console.log("cleanup: "+snapshot.numChildren()+" activities");
    var updates = {};
    snapshot.forEach((child) => {
      updates["activities/"+child.key] = null;
    });
    admin.database().ref().update(updates).then(() => {
      res.status(200).send(snapshot.numChildren()+" activities deleted");
    }).catch((error) => {
      res.status(500).send(error);
    })
  });
});

了解详情:

  • Firebase documentation on querying
  • Firebase blog post on multi-location updates

这篇关于如何使用Firebase Cloud功能在DataSnapshot中查找特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆