【发布时间】: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