如何比较来自数据库和请求的 Firestore 规则中的日期
How to Compare Dates In Firestore Rules From Database And Request
最近我在一个平台上工作时遇到了一个问题,我想比较存储在用户数据库中的时间戳和 Firebase 规则中的请求时间。这是为了只有在 He/She 仅在特定时间后请求时才能阅读文档。
这是我到目前为止尝试使用的代码。
注意:字段名strton
指的是数据库文档中的时间戳对象。
match /tests/{testsID}{
match /response/{responseID}{
allow read: if get(/databases/$(database)/documents/tests/{testID}).data.strton < request.time;
}
}
match /tests/{testsID}{
match /response/{responseID}{
allow read: if get(/databases/$(database)/documents/tests/{testID}).data.strton.toMillis() < request.time.toMillis();
}
}
感谢任何有关如何实现此目标的帮助。
更新:
这是文档结构(私人信息已被审查)
以及试图访问它的代码
docRef = doc(db, "tests", testid,"responses",auth.currentUser.uid);
docSnap = await getDoc(docRef);
if (docSnap.exists()) {
testResponseList = docSnap.data()
}else {
await setDoc(doc(db, "tests", testid,"responses",auth.currentUser.uid), {
answers:[]})
还有一件事是此代码试图获取的文档目前不存在。而 else 语句是为了使文件不存在。但是else语句没有执行。
您正在使用 {wildcard}
文档路径中使用的 /match
表达式语法 get()
。相反,您应该使用 $(testsID)
.
还有一些拼写错误:
- 你的 collection 名字是
responses
但你有 response
规则
- 通配符是
testsID
但你有 testID
尝试重构规则,如下所示:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /tests/{testID} {
match /responses/{responseID} {
allow read: if get(/databases/$(database)/documents/tests/$(testID)).data.strton < request.time;
}
}
}
}
最近我在一个平台上工作时遇到了一个问题,我想比较存储在用户数据库中的时间戳和 Firebase 规则中的请求时间。这是为了只有在 He/She 仅在特定时间后请求时才能阅读文档。
这是我到目前为止尝试使用的代码。
注意:字段名strton
指的是数据库文档中的时间戳对象。
match /tests/{testsID}{
match /response/{responseID}{
allow read: if get(/databases/$(database)/documents/tests/{testID}).data.strton < request.time;
}
}
match /tests/{testsID}{
match /response/{responseID}{
allow read: if get(/databases/$(database)/documents/tests/{testID}).data.strton.toMillis() < request.time.toMillis();
}
}
感谢任何有关如何实现此目标的帮助。
更新:
这是文档结构(私人信息已被审查)
docRef = doc(db, "tests", testid,"responses",auth.currentUser.uid);
docSnap = await getDoc(docRef);
if (docSnap.exists()) {
testResponseList = docSnap.data()
}else {
await setDoc(doc(db, "tests", testid,"responses",auth.currentUser.uid), {
answers:[]})
还有一件事是此代码试图获取的文档目前不存在。而 else 语句是为了使文件不存在。但是else语句没有执行。
您正在使用 {wildcard}
文档路径中使用的 /match
表达式语法 get()
。相反,您应该使用 $(testsID)
.
还有一些拼写错误:
- 你的 collection 名字是
responses
但你有response
规则 - 通配符是
testsID
但你有testID
尝试重构规则,如下所示:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /tests/{testID} {
match /responses/{responseID} {
allow read: if get(/databases/$(database)/documents/tests/$(testID)).data.strton < request.time;
}
}
}
}