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
:
What might be causing this error?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With