【发布时间】:2021-03-01 17:23:46
【问题描述】:
我有一个Questions 收藏。里面是一个嵌套集合Responses。因此,可以在该特定文档中找到对特定问题的所有回答。
我必须限制用户对同一个问题发送多个回复。
目前,我在服务器端 (admin-SDK) 上执行此操作,方法是将用户的 uid 保存在响应文档中。
firestore
.collection("questions")
.doc(questionID)
.collection("responses")
.where("uid", "==", AuthUser.id)
.get().then(snapshot => {
if (snapshot.docs.length == 0) {
let createdAt = Date.now()
firestore
.collection("questions")
.doc(questionID)
.collection("responses")
.add({
uid: AuthUser.id,
answer,
createdAt,
updatedAt: createdAt
}).then(() => {
return res.status(200).json({ msg: 'Success' })
})
} else {
return res.status(400).json({ error: 'You cannot send more than one response' })
}
})
有没有办法通过 firebase 安全规则实现这种行为?
【问题讨论】:
标签: firebase google-cloud-firestore firebase-security