从Firebase检索所有数据,但不包括某些字段 [英] Retrieving all the data from firebase excluding some fields

查看:80
本文介绍了从Firebase检索所有数据,但不包括某些字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angular,firebase和cloud函数进行项目.我的数据如下:

I am working on a project using angular,firebase and cloud functions.I have data as follows:

我想检索除profile_picture字段之外的所有数据.在云功能中:

I want to retrieve all the data excluding profile_picture field.In cloud function:

    exports.getPartner = functions.https.onRequest((req, res) => {
    res.header('Content-Type', 'application/json');
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type');


    if (req.method === 'OPTIONS') {
        res.status(204).send('');
    }


    var partner = admin.database().ref().child('partner');
    partner.on("value", function (snapshot) {
        res.status(200).json({ partner: snapshot.val() });
    }, function (errorObject) {
        console.log("The read failed: " + errorObject.code);
    });
});

云函数/角度中是否有任何方法可以排除某些字段,以便更快地获取数据? 任何帮助表示赞赏.

Is there any way in cloud function/in angular to exclude some field so that data fetching will be faster? Any help is appreciated.

推荐答案

从Firebase实时数据库(即从特定节点)获取数据时,您将在包括子集合(如果有)的位置下载整个数据. 请参阅Firebase文档以获取值

While fetching data from firebase real time database (i.e from particular node) you will download entire data at the location including the child collection if you have. See firebase doc for listening values

有两种方法可以避免使用profile_picture

There are two ways you can avoid the profile_picture

  1. 在将响应返回到https调用时,您可以消除个人资料图片密钥.
  2. 否则,如果在下载合作伙伴树时不愿意,则将个人资料图片放在其他树中.

让我在下面详细解释

步骤1:从对象中删除个人资料图片

var partner = admin.database().ref().child('partner');
partner.on("value", function (snapshot) {
    res.status(200).json({ partner: snapshot.val() }); 
    // don't simply return like this. Instead follow like below
   var partners = [] ; // create a partners array
  snapshot.forEach(function(childSnap){
      var p = childSnap.val();
      p.id = childSnap.key // this will the id of the each child inside the partner tree
      delete p.profilePicture;
      partners.push(p); // 
  });
  res.status(200).json({ partner: partners }); 
}, function (errorObject) {
    console.log("The read failed: " + errorObject.code);
});

如果您像下面这样操作,则可以从https响应中删除个人资料图片密钥.但是请记住,无论如何,您都将从树上下载所有数据.

If you do like below you can eliminate the profile picture key from the https response. But keep one thing mind, anyway you'll download all data from the tree.

第2步:将个人资料图片放入其他树中

为此,您需要更改将数据添加到伙伴树的方法.让我解释一下.

To do this, you need to change the method which adds the data to the partners tree. Let me explain this.

例如:在添加合作伙伴数据时,将profile_picture放置在诸如partnerDetails树之类的其他名称中.

For Example : While adding the partners data put the profile_picture in some other like partnerDetails tree.

var partnerKey = firebase.database().ref('partner').push().key;
var partnerUpdate = {};
partnerUpdate['partner/' + partnerKey] = partner; // parnter Object without profilepicture
partnerUpdate['partnerDetails/' + partnerKey + 'profilePicture'] = profilePicture; 
// the partnerKey must be same for the both the tree because it's the associated key for both of them. 

firebase.database().ref().update(partnerUpdate) // this is called multipath update

因此,执行完此操作后,您的合作伙伴树中将不会显示个人资料图片.我希望您在客户端中列出合作伙伴树,然后从列表中单击一个合作伙伴,这将显示整个详细信息.如果这样做,您可以传递合作伙伴密钥(即关联密钥)并获取合作伙伴资料图片,

So after doing like this you wont have the profile picture in the partner tree. I hope you list the partners tree in the client and click one of the partner from the list which will display the entire details. If you doing so, you can pass the partners key (i.e.associated key) and get the partners profile picture to do so,

firebase.database().ref('/partnerDetails/' + partnerId).on('value').then(function(partDetailSnap){
  // here you will get the profile_picture of the partner. 
});

云功能将非常快.除非需要冷启动.您可以对其他一些字段执行相同的操作,就像我使用步骤2或1对个人资料图片所做的一样.

Cloud Functions will be really fast. Unless it needs cold start. You can do same for the some other fields like i did it for the profile picture using step 2 or 1.

希望这会给您一些想法.随时问更多.

Hope this gives some idea. Feel free to ask more.

这篇关于从Firebase检索所有数据,但不包括某些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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