'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;
        });
});


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tamás Sengel