Home > Software engineering >  How to get a document from firestore in cloud functions
How to get a document from firestore in cloud functions

Time:03-04

I've tried many methods such as

const admin = require("firebase-admin");
admin.initializeApp();

const db = admin.firestore();

const docRef = db.collection("users").doc(dynamicDocID).get()
const docRef = db.collection("users").doc(dynamicDocID)



as well as many other and keep getting undefined or a promise that never seems to be resolved

Cant seem to find proper docs on this if anything

CodePudding user response:

Since Cloud Functions for Firebase are written in Node.js, have a look at the Node.js examples in the Firestore documentation.

Based on that:

const docRef = db.collection("users").doc(dynamicDocID)
const document = await docRef.get()
console.log(document.id, document.data())

Or if you can't use await:

const docRef = db.collection("users").doc(dynamicDocID)
return docRef.get().then((document) => {
  console.log(document.id, document.data())
})
  • Related