在Cloud函数中,如何从另一个集合中加入以获取数据? [英] In Cloud function how can i join from another collection to get data?

查看:51
本文介绍了在Cloud函数中,如何从另一个集合中加入以获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cloud Function向移动设备发送通知.我在Firestore clientDetailclientPersonalDetail中有两个集合.我在两个集合中都具有clientID相同的内容,但日期存储在clientDetail中,名称存储在clientPersonal中.

I am using Cloud Function to send a notification to mobile device. I have two collection in Firestore clientDetail and clientPersonalDetail. I have clientID same in both of the collection but the date is stored in clientDetail and name is stored in clientPersonal.

看看:

ClientDetail -- startDate
             -- clientID
             .......

ClientPersonalDetail -- name
                     -- clientID
                     .........

这是我的完整代码:

exports.sendDailyNotifications = functions.https.onRequest(  (request, response) => {
var getApplicants = getApplicantList();
console.log('getApplicants', getApplicants);

cors(request, response, () => {
  admin
    .firestore()
    .collection("clientDetails")
    //.where("clientID", "==", "wOqkjYYz3t7qQzHJ1kgu")
    .get()
    .then(querySnapshot => {
      const promises = [];
      querySnapshot.forEach(doc => {
        let clientObject = {};
        clientObject.clientID = doc.data().clientID;
        clientObject.monthlyInstallment = doc.data().monthlyInstallment;
        promises.push(clientObject);
      });

      return Promise.all(promises);
    }) //below code for notification
    .then(results => {
      response.send(results);
      results.forEach(user => {
        //sendNotification(user);
      });
      return "";
    })
    .catch(error => {
      console.log(error);
      response.status(500).send(error);
    });
});

} );

以上功能显示的是这样的对象

Above function is showing an object like this

{clienId:xxxxxxxxx, startDate:23/1/2019}

但是我不需要ClientID来显示在通知中,因此我必须加入clientPersonal集合才能使用clientID来获取名称. 应该怎么办?

But I need ClientID not name to show in notification so I'll have to join to clientPersonal collection in order to get name using clientID. What should do ?

如何创建另一个函数,该函数仅通过将clientID作为参数传递来返回名称,并等待它返回name. 有人可以帮忙吗??

How can I create another function which solely return name by passing clientID as argument, and waits until it returns the name . Can Anybody please Help.?

推荐答案

在这种情况下,您可以使用两个额外的功能来使代码清晰.一个将从集合ClientDetail中读取所有文档的函数,而不是获取所有字段,而只会获取ClientID.然后调用另一个函数,该函数将扫描集合ClientPersonalDetail中的所有文档,并仅检索具有ClientID的零件.比较这两个是否匹配,然后在匹配的地方进行任何操作.

You can use two extra functions in this occasion to have your code clear. One function that will read all the documents form the collection ClientDetail and instead of getting all the fields, will get only the ClientID. Then call the other function, that will be scanning all the documents in collection ClientPersonalDetail and retrieve only the part with the ClientID. Compare if those two match and then do any operations there if they do so.

您可以参考 Cloud Firestore入门文档,了解如何创建,从Firestore添加和加载文档.

You can refer to Get started with Cloud Firestore documentation on how to create, add and load documents from Firestore.

您的package,json应该看起来像这样:

Your package,json should look something like this:

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "firebase-admin": "^6.5.1"
  }
}

我自己做了一些编码,这是我的

I have did a little bit of coding myself and here is my example code in GitHub. By deploying this Function, will scan all the documents form one Collection and compare the ClientID from the documents in the other collection. When it will find a match it will log a message otherwise it will log a message of not matching IDs. You can use the idea of how this function operates and use it in your code.

这篇关于在Cloud函数中,如何从另一个集合中加入以获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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