簡體   English   中英

如何從 Firestore 獲取所有文檔並進行循環(雲功能)

[英]how to get all documents from Firestore and make a loop (Cloud Functions)

我正在嘗試使用雲 function 使用 Javascript 訪問我的 Firestore 數據庫中的數據我需要循環我擁有的所有文檔(我有 7 個文檔),但它只返回第一個文檔並將其打印在日志中,我不不知道為什么。 這是我的代碼

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

注意:它在頁面中打印“Hello Firebase”,並在日志中打印“hello logs”和第一個文檔 Id

通過執行response.send() ,您實際上是在終止您的雲 Function,請參閱 文檔

如果要記錄所有文檔 ID,請執行以下操作:

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");

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM