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

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

问题描述

我正在使用云功能向移动设备发送通知.我在 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 ,并等到它返回 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 应如下所示:

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

我自己做了一些编码,这是我的 GitHub 中的示例代码.通过部署此函数,将扫描一个集合中的所有文档,并比较另一个集合中文档的 ClientID.当它找到匹配项时,它将记录一条消息,否则它将记录一条不匹配 ID 的消息.您可以使用此函数如何操作的想法并在您的代码中使用它.

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.

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

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