Get Firestore data from a Cloud Function

Akash Joshi
Feb 5 · 1 min read
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

Akash Joshi

Written by

🚀FullStack 🖥️OpenSource ✍️Writing

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade