Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: admin is not defined using HTTP request with Cloud Functions for Firebase

I just configured my first Cloud Functions for Firebase and after deploying the index.js file and upload all the changes to my server. I'm trying to use one of my functions which is a simple HTTP request to create a field on a table in my Firebase Database:

const functions = require('firebase-functions');

exports.addMessage = functions.https.onRequest((req, res) => {
        // Grab the text parameter.
        const original = req.query.text;
        // Push it into the Realtime Database then send a response
        admin.database().ref('/messages').push({ original: original }).then(snapshot => {
            // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
            res.redirect(303, snapshot.ref);
        });
    });

But when I'm calling to my request on my browser I'm receiving an error on my Functions console which says admin is not defined:

enter image description here

What might be causing this error?

like image 317
Francisco Durdin Garcia Avatar asked Jan 05 '23 03:01

Francisco Durdin Garcia


1 Answers

You need to import the Admin SDK module just like you are importing the Cloud Functions SDK:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

Example code can be found here. This is also covered in the "get started" guide.

like image 166
Jeff Avatar answered May 16 '23 06:05

Jeff