簡體   English   中英

使用 Firebase 函數從 Firestore 集合中獲取所有文檔

[英]Get all documents from Firestore Collection using Firebase Functions

我試圖在我的 Firebase Function 中的一個名為“repeated_tasks”的集合中獲取所有文檔。

我嘗試使用以下代碼: Is it possible to get all documents in a Firestore Cloud Function? ,但這似乎對我不起作用。 我正在嘗試獲取信息,以便可以更新集合中的每個文檔,將一個字段設置為 false。 我有以下代碼:

exports.finishedUpdate = functions.pubsub.schedule('0 3 * * *').timeZone('Europe/Amsterdam').onRun((context) => {

//  This is part of the above mentioned stack question
    var citiesRef = database.collection('repeated_tasks');
    const snapshot = citiesRef.get();
    snapshot.forEach(doc => {
      console.log(doc.id, '=>', doc.data());
    });

// A way to update all of the documents in the repeated_tasks collection has to be found

//  This part works, for only the two given document ids
    var list = ['qfrxHTZAJZTJDQpA83fjsM03159438695', 'qfrxHTZAJZQTpM3pA83fjsM0315217389'];

    for (var i = 0; i < list.length; i++) {
        database.doc('repeated_tasks/' + list[i]).update({'finished': false});
    }

    return console.log("Done");
})

非常感謝幫助,因為我似乎無法在任何地方找到任何相關信息,除了堆棧溢出頁面,它沒有幫助。 我正在使用 Node JS (Javascript) 來設置功能。

通過使用從 Firestore 獲取信息的語法,我還能夠在 Firebase 函數中更新它,並且可以使用以下代碼更新所有信息:

    const reference = database.collection('repeated_tasks/');
    const snapshot = await reference.where('finished', '==', true).get();
    if (snapshot.empty) {
        console.log('no matching documents');
        return;
    }

    snapshot.forEach(doc => {
        database.doc('repeated_tasks/' + doc.id).update({'finished': false});
    });

暫無
暫無

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

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