How to create a scheduled function that reads data from Firebase and then sends a message on twilio

Viewed 62

Im am trying to read data off my database and then send a message through Twilio. What I am expecting this function to do is to check the database every minute. it will check all the documents inside the players collection. If the sessions field in one of these documents is equal to 0, then it creates a message and sends it to the phone number in that same document.

here is my current code:

const account = '***********************************';
const auth = '********************************';
const client = require('twilio')(account,auth);
const firebaseFunc = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const database = admin.firestore()

exports.paymentChecker = firebaseFunc.pubsub.schedule('* * * * *').onRun((context) => {
    database.collection('Players').get()
    .then(querySnapshot => {
        querySnapshot.forEach(doc => {
            if(doc.data().sessions, '==', 0){
                client.messages.create({
                    to: doc.data().phone,
                    from: '+***********',
                    body: 'Hi' + doc.data().First + ', you have to renew your registration',
                })
            }
        })
    }).catch()
})

however, this code is not deploying to firebase as it is yielding a number of errors.

19:5   error  Expected catch() or return                  promise/catch-or-return
  20:11  error  Each then() should return a value or throw  promise/always-return
  22:16  error  Unexpected constant condition               no-constant-condition

how to resolve this? thanks

2 Answers

The problem comes from the fact that you are not returning the Promises returned by the asynchronous methods (get() and client.messages.create()). See the doc for more details on how it is important, in addition to the code errors it generates.

So the following should do the trick (untested):

exports.paymentChecker = firebaseFunc.pubsub.schedule('* * * * *').onRun((context) => {
    return database.collection('Players').get()
        .then(querySnapshot => {
            const promises = [];
            querySnapshot.forEach(doc => {
                if (doc.data().sessions == 0) {
                    promises.push(
                        client.messages.create({
                            to: doc.data().phone,
                            from: '+***********',
                            body: 'Hi' + doc.data().First + ', you have to renew your registration',
                        })
                    );
                }
            })
            return Promise.all(promises);
        }).catch(error => {
            console.log(error);
            return null;
        })
});

Also note that if(doc.data().sessions, '==', 0) cannot work. Like the other errors, you could have detected it by noting the line number mentioned at the beginning of the corresponding error line: 22:16 error Unexpected constant condition no-constant-condition).

Related