我正在使用firebase函数并成功运行了一个清理函数。
我遇到的问题是我的firebase数据库看起来像这样。
当创建帖子时,ABC123和0l692lPD6EfqUZ4Y4xiCOVmnNmC2都会自动设置,所以我不知道它们会是什么。
我可以硬编码第一个ABC123并为第二个设置{postID}并且它成功运行。但是,如果我将两者都设置为posteId则失败。
下面是我的代码。我需要能够将ABC123更改为postId,因为我不知道它在index.js代码中会是什么。
exports.sanitizePost = functions.database
.ref('/posts/ABC123/question/{postId}')
.onWrite(event => {
const post = event.data.val()
if (post.sanitized) {
return
}
console.log("Sanitizing new post " + event.params.pushId)
console.log(post)
post.sanitized = true
post.question = sanitize(post.question)
const promise = event.data.ref.set(post)
return promise
})
function sanitize(s) {
var sanitizedText = s
sanitizedText = sanitizedText.replace(/\bstupid\b/ig, "wonderful")
return sanitizedText
}
我需要.ref('/posts/ABC123/question/{postId}')
为.ref('/posts/{postID}/question/{postId}')
或有效的东西。
答案 0 :(得分:1)
路径中不能两次使用相同的参数。但是你可以有两个不同名称的参数,例如
exports.sanitizePost = functions.database
.ref('/posts/{questionId}/question/{postId}')