Get Firestore data from a Cloud Function

A J
1 min readFeb 5, 2019

--

Cloud Firestore Image

First, initialize admin and db,

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

And the function you want to export,

exports.getData = functions.https.onRequest((req, res) => {
const docRef = db.collection('collection name').doc('document name');
const getDoc = docRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
return res.send('Not Found')
}
console.log(doc.data());
return res.send(doc.data());
})
.catch(err => {
console.log('Error getting document', err);
});
});

That’s it 😄 !

Get in touch !!

Github LinkedIn Twitter

--

--