如何使用云功能在Firestore中删除文档

时间:2020-10-30 20:03:39

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

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

pauseMenuUI

数据库结构:

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

云功能是使用javascript编写的

2 个答案:

答案 0 :(得分:2)

您可以只使用#include <iostream> #include <array> using namespace std; int main() { array<int, 5> age{11,2,23,4,15}; for(int val : age) { cout << val << " "; } cout << endl; } 来获取ref并致电delete()

DocumentReference

如果他们以后可以编辑其帖子,则应将其通用化,并将其添加到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(); } } ) 触发器中

答案 1 :(得分:1)

这个答案将很简短,但是不需要解释。这是代码(为了快速起见,我不确定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);
});

希望有帮助