I am trying to access data in my Firestore database using cloud function using Javascript I need to loop on all the documents I have (I have 7 documents), but it only returns the first document and prints it in the logs, and I don't know why. this is my code
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// Create and deploy your first functions
// https://firebase.google.com/docs/functions/get-started
exports.helloWorld = functions.https.onRequest(async (request, response) => {
functions.logger.info("Hello logs", {structuredData: true});
//loop on the document
const facultyRef = await admin.firestore().collection('faculty').get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
response.send("Hello Firebase");
//print the doc id in the logs
functions.logger.info(doc.id);
});
});
});
Note: It print 'Hello Firebase' in the page, and print 'hello logs' and only the first document Id in the logs
CodePudding user response:
By doing response.send()
you are actually terminating your Cloud Function, see the documentation.
If you want to log all the doc IDs, do as follows:
exports.helloWorld = functions.https.onRequest(async (request, response) => {
functions.logger.info("Hello logs", { structuredData: true });
const facultyQuerySnapshot = await admin.firestore().collection('faculty').get();
facultyQuerySnapshot.forEach((doc) => {
//print the doc id in the logs
functions.logger.info(doc.id);
});
// We terminate the Function AFTER the loop has completed
response.send("Hello Firebase");
});