1

I am trying to query my firestore database using cloud functions. I want to trigger an email notification every time a new reading in my database is under the value of 10.

Here is the relevant database structure for reference: database structure. The "readings" field is an array and each "reading" is a map which holds the fields "date" and "value".

Currently I am at the point where I can send an email notification every time a new user is created however I want this to work for the database. I am unsure how to query for the "readings" array and then for each individual reading.

Here is my code so far which sends an email when a new user is created

exports.sendNotification = functions.auth.user().onCreate((user) => {


const mailOptions = {
    from: '"Spammy Corp." <[email protected]>',
    to:"[email protected]",
    text: "TEST"
};

return mailTransport.sendMail(mailOptions)
    .then(() => console.log("It worked"))
    .catch((error) => 
console.error('There was an error while sending the email:', error)); 
});
3
  • I suggest you use the documentation to make an attempt at querying the database and deciding how to react when something changes. Right now it looks like you're just asking someone to write the code for you. Commented Dec 5, 2018 at 17:54
  • Thank you for your feedback so far, I have had a look at the documentation but on my own I was only able to get it working when a new sensor was created so far using .document{sensor/{sensorId}... I reassure you I have been working on this problem for days now - what really confused me what the fact the "readings" is an array. Im sorry if my question came across as lazy!! Commented Dec 5, 2018 at 18:16
  • Then you should look into using onUpdate instead of onCreate. Commented Dec 5, 2018 at 19:45

1 Answer 1

2

See: https://firebase.google.com/docs/firestore/extend-with-functions

For example, to fire on all new readings added to that first child:

exports.sendEmail = functions.firestore
    .document('sensor/UGt.../readings')
    .onCreate((snap, context) => {
        const newValue = snap.data();
        const value = newValue.value;
        if (value < 10) {
            // send email
        }
    });

In further comments you mentioned listening for new readings in all sensor elements, not just your first one. This is unfortunately not possible in an efficient / simple way (source). Instead you will have to listen to all onUpdate events on /sensor/, check if the update is adding a reading, then check the value & send your email.

It may be easier to call the cloud function directly from wherever adds the reading, depending on how many times the /sensor/ path is going to be updated for other reasons (since every time this happens, it's a waste of resources).

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for you help thus far this is a big help to me already - I want to trigger a new email when a new reading is added.
So the code above will trigger when new readings are added to the first "sensor" object. Do you want the emails to be sent when new readings are added to any of the sensors (the long GUIDs)?
Thanks for you clarification. my apologies. You are correct, I was intending the email to be sent when a new reading below 10 is added to any sensor!
@KabiraSuleman Thanks. Have added a lot more information to my answer, unfortunately it's not great news..!
I appreciate your help so much, I will work on this now!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.