Q: How to fix Cloud Function error admin.database.ref is not a function at exports I'm currently trying to modify my Cloud Functions and move in under https.onRequest so that i can call use it to schedule a cron job. How it i'm getting the following error in the logs. 


TypeError: admin.database.ref is not a function
        at exports.scheduleSendNotificationMessageJob.functions.https.onRequest (/user_code/index.js:30:20)
        at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)


exports.scheduleSendNotificationMessageJob = functions.https.onRequest((req, res) => {
    admin.database.ref('/notifications/{studentId}/notifications/{notificationCode}')
        .onCreate((dataSnapshot, context) => {
            const dbPath = '/notifications/' + context.params.pHumanId + '/fcmCode';
            const promise = admin.database().ref(dbPath).once('value').then(function(tokenSnapshot) {
                const theToken = tokenSnapshot.val();
                res.status(200).send(theToken);
                const notificationCode = context.params.pNotificationCode;
                const messageData = {notificationCode: notificationCode};
                const theMessage = {    data: messageData,
                    notification: { title: 'You have a new job reminder' }
                };
                const options = {   contentAvailable: true,
                    collapseKey: notificationCode };
                const notificationPath = '/notifications/' + context.params.pHumanId + '/notifications/' + notificationCode;
                admin.database().ref(notificationPath).remove();
                return admin.messaging().sendToDevice(theToken, theMessage, options);
            });
            return null;
        });
});


A: You cannot use the definition of an onCreate() Realtime Database trigger within the definition of an HTTP Cloud Function.
If you switch to an HTTP Cloud Function "so that (you) can call use it to schedule a cron job" it means the trigger will be the call to the HTTP Cloud Function. In other words you will not be anymore able to trigger an action (or the Cloud Function) when new data is created in the Realtime Database.
What you can very well do is to read the data of the Realtime Database, as follows, for example (simplified scenario of sending a notification):
exports.scheduleSendNotificationMessageJob = functions.https.onRequest((req, res) => {

    //get the desired values from the request
    const studentId = req.body.studentId;
    const notificationCode = req.body.notificationCode;

    //Read data with the once() method
    admin.database.ref('/notifications/' + studentId + '/notifications/' + notificationCode)
     .once('value')
     .then(snapshot => {
         //Here just an example on how you would get the desired values
         //for your notification
         const theToken = snapshot.val();
         const theMessage = ....
         //......

         // return the promise returned by the sendToDevice() asynchronous task
         return admin.messaging().sendToDevice(theToken, theMessage, options)
      })
      .then(() => {
         //And then send back the result (see video referred to below)
         res.send("{ result : 'message sent'}") ;
      })
      .catch(err => {
         //........
      });

});

You may watch the following official Firebase video about HTTP Cloud Functions: https://www.youtube.com/watch?v=7IkUgCLr5oA&t=1s&list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM&index=3. It shows how to read data from Firestore but the concept of reading and sending back the response (or an error) is the same for the Realtime Database. Together with the 2 other videos of the series (https://firebase.google.com/docs/functions/video-series/?authuser=0), it also explains how it is important to correctly chain promises and to indicate to the platform that the work of the Cloud Function is finished.

A: For me, this error happened when writing admin.database instead of admin.database().
