【问题标题】:How to delete a document in Firestore using cloud functions如何使用云功能删除 Firestore 中的文档
【发布时间】:2021-02-13 07:25:22
【问题描述】:

我想检查在 Firestore 中创建的文档,以确保公开可见的字段中不包含脏话。在检测到该帖子包含脏话后,我希望能够删除该帖子。为此,我正在尝试使用 firebase 云功能:

// Package used to filter profanity
const badWordsFilter = require('bad words-list');

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

export const filterBadWords =
  functions.firestore.document("posts/{userId}/userPosts/{postId}").onCreate((snapshot, context) => {
    const message = snapshot.data.val();

    // Check if post contains bad word
    if (containsSwearwords(message)) {
      //This is where I want to remove the post.
      //What do I put here to delete the document?
    }
  })

// Returns true if the string contains swearwords.
function containsSwearwords(message: any) {
  return message !== badWordsFilter.clean(message);
}

数据库结构:

-posts(Collection)
   |
   +---{userID} (Document)
          |
          +---userPosts (collection)
                  |
                  +---{Documents to be checked} (Document)
                  |
                  +-Url1(field)
                  +-Url2(field)
                  +-string1(field)<- check this field for swear
                  +-string2(field)<- check this field for swear

云函数是使用 javascript 编写的

【问题讨论】:

  • 现代版本的 firebase-functions 库不会将名为 event 的东西传递给 onCreate 函数的第一个参数。他们提供文档快照,如文档中所示。如果您使用的是旧版本,它真的旧了,您应该升级。 firebase.google.com/docs/functions/…
  • 您使用的是 web、swift、objective-c、java android、kotlin/KTX android、java、python 还是 go?
  • @Baby_Boy 我用的是swift,但是函数是用javascript写的

标签: javascript swift typescript firebase google-cloud-firestore


【解决方案1】:

这个答案会很短,但不需要解释。这是代码(对于 swift,我不是 100% 确定如何在 js 中执行此操作)

迅速:

db.collection("cities").document("DC").delete() { err in
    if let err = err {
        print("Error removing document: \(err)")
    } else {
        print("Document successfully removed!")
    }
}

这可能适用于 javascript,但不是 100% 确定

db.collection("cities").doc("DC").delete().then(function() {
    console.log("Document successfully deleted!");
}).catch(function(error) {
    console.error("Error removing document: ", error);
});

希望对你有帮助

【讨论】:

    【解决方案2】:

    您可以使用ref 获取DocumentReference 并致电delete()

    functions.firestore.document("posts/{userId}/userPosts/{postId}").onCreate(
      async (snapshot, context) => {
        const message = snapshot.data.val();
    
        // Check if post contains bad word
        if (containsSwearwords(message)) {
          await snapshot.ref.delete();
        }
      }
    )
    

    如果他们之后可以编辑他们的帖子,您应该概括此功能并将其添加到 onUpdate 触发器中

    【讨论】:

    • 这看起来像我要找的,但我收到错误 error Parsing error: Unexpected token snapshot
    • snapshot 是您在onCreate 监听器中获得的属性
    • 是的,我知道我拥有它,就像您拥有它一样。我现在正在排除故障。
    • 没关系,缺少异步。现在工作。我会奖励赏金
    • @Bjorn 很高兴看到它有效。是的,我应该提到我在那里使用了async/await 模式:)
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2018-04-23
    • 2022-01-15
    • 2019-08-07
    • 2021-08-09
    • 1970-01-01
    • 2018-08-01
    • 2018-05-19
    相关资源
    最近更新 更多