Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用云函数删除Firestore中的文档_Javascript_Swift_Typescript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 如何使用云函数删除Firestore中的文档

Javascript 如何使用云函数删除Firestore中的文档,javascript,swift,typescript,firebase,google-cloud-firestore,Javascript,Swift,Typescript,Firebase,Google Cloud Firestore,我想检查在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 trigger

我想检查在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

-帖子(收藏)
|
+---{userID}(文档)
|
+---用户帖子(收集)
|
+---{要检查的文档}(文档)
|
+-Url1(字段)
+-Url2(字段)

+-string1(field)您可以使用
ref
获取
DocumentReference
并调用:


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

此答案将很简短,但不需要解释。下面是代码(对于swift,我不能100%确定如何在js中做到这一点)

斯威夫特:

db.collection(“cities”).document(“DC”).delete(){err in
如果让err=err{
打印(“删除文档时出错:\(错误)”)
}否则{
打印(“文档已成功删除!”)
}
}
这可能适用于javascript,但不是100%确定

db.collection(“cities”).doc(“DC”).delete().then(function()){
log(“文档已成功删除!”);
}).catch(函数(错误){
console.error(“删除文档时出错:”,错误);
});

希望这有助于

firebase函数库的现代版本不会将名为
事件的内容传递给onCreate函数的第一个参数。它们提供文档快照,如文档中所示。如果您使用的是旧版本,它确实很旧,您应该升级。你用的是web、swift、objective-c、java android、kotlin/KTX android、java、python还是go?@Baby_Boy我用的是swift,但是函数是用Javascript编写的这看起来像我要找的,但是我得到了错误
错误解析错误:意外的令牌快照
快照
是您在
onCreate
列表中得到的属性是的,我看到它与您在那里的方式一样。我现在正在进行故障排除。Nevermind缺少异步。现在工作。当我can@Bjorn很高兴看到它起作用了。是的,我应该提到我在那里使用了
async/await
模式:)
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();
    }
  }
)