How to get a document from firestore in cloud functions

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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())
})


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x