Get Firestore data from a Cloud Function

Image for post
Image for post
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

Written by

🚀FullStack 🖥️OpenSource ✍️Writing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store