Ad

Using Cloud Firestore In Cloud Function

Cloud Function Documentation says

// The Firebase Admin SDK to access the Firebase Realtime Database. const admin = require('firebase-admin'); admin.initializeApp();

I am using Cloud Firestore, not Realtime Database. How do I access it?

Ad

Answer

Use admin.firestore() like for example:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = requi re('firebase-functions');

// The Firebase Admin SDK
const admin = require('firebase-admin');
admin.initializeApp();


//Write a function that uses Firestore, e.g.: 

exports.anHttpCloudFunction = functions.https.onRequest((req, res) => {

   admin.firestore().collection('XYZ').doc('ABC').set(....)
   .then(() => {
       res.send({ msg: 'Success' });
   })
   .catch(err => {....})

});
Ad
source: stackoverflow.com
Ad