簡體   English   中英

如何在 angularfire firestore 中刪除整個文檔集合

[英]How to do remove an entire document collection in angularfire firestore

我剛剛更改了我的代碼,如下所示,它正在工作。 在 google cloud-firestore 中可以批量刪除。

async deleteMembersOfGroup(clubId: string, groupId: string) {
    //path of the collection
    const path = "club/" + clubId + "/group/" + groupId + "/group-members";

//query snapshot
    const qry: firebase.firestore.QuerySnapshot = await this.afs.collection(path).ref.get();

    const batch = this.afs.firestore.batch();

//looping through docs in the collection to delete docs as a bulk operation
    qry.forEach(doc => {
      console.log('deleting....', doc.id);
      batch.delete(doc.ref);
    });

//finally commit
    batch.commit().then(res => console.log('committed batch.'))
    .catch(err => console.error('error committing batch.', err));
  }

文檔說:

不建議從 Web 客戶端刪除 collections。

因此省略給出示例(盡管提供了 Node.js 示例)。

因此,您可以調整 Node.js 代碼以在 JavaScript 中工作(盡管 Google 不建議這樣做),或者您可以使用雲 function 解決方案(如果您使用的是 Google 提供的雲功能)。

使用 Callable Cloud 刪除集合 Function

這是直接來自文檔的雲 function 代碼:

雲 FUNCTION 代碼

/**
 * Initiate a recursive delete of documents at a given path.
 * 
 * The calling user must be authenticated and have the custom "admin" attribute
 * set to true on the auth token.
 * 
 * This delete is NOT an atomic operation and it's possible
 * that it may fail after only deleting some documents.
 * 
 * @param {string} data.path the document or collection path to delete.
 */
exports.recursiveDelete = functions
  .runWith({
    timeoutSeconds: 540,
    memory: '2GB'
  })
  .https.onCall(async (data, context) => {
    // Only allow admin users to execute this function.
    if (!(context.auth && context.auth.token && context.auth.token.admin)) {
      throw new functions.https.HttpsError(
        'permission-denied',
        'Must be an administrative user to initiate delete.'
      );
    }

    const path = data.path;
    console.log(
      `User ${context.auth.uid} has requested to delete path ${path}`
    );

    // Run a recursive delete on the given document or collection path.
    // The 'token' must be set in the functions config, and can be generated
    // at the command line by running 'firebase login:ci'.
    await firebase_tools.firestore
      .delete(path, {
        project: process.env.GCLOUD_PROJECT,
        recursive: true,
        force: true,
        token: functions.config().fb.token
      });

    return {
      path: path 
    };
  });

客戶端代碼

/**
 * Call the 'recursiveDelete' callable function with a path to initiate
 * a server-side delete.
 */
function deleteAtPath(path) {
    var deleteFn = firebase.functions().httpsCallable('recursiveDelete');
    deleteFn({ path: path })
        .then(function(result) {
            logMessage('Delete success: ' + JSON.stringify(result));
        })
        .catch(function(err) {
            logMessage('Delete failed, see console,');
            console.warn(err);
        });
}

一如既往,為什么 GCP 這么難!

暫無
暫無

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

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