Search code examples
node.jsfirebasepush-notificationgoogle-cloud-firestoregoogle-cloud-functions

In Cloud function how can i join from another collection to get data?


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.

Take a look:

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

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

Here is My full Code:

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}

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 ?

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.?


Solution

  • 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.

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

    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.