我正在尝试在 Firestore 数据库上编写安全规则,以允许读取 status
值为 published
的集合中的文档。但是,一旦规则发布,就不允许读取该集合的任何文档。
我遵循官方文档中所述的权限设置。 https://firebase.google.com/docs/firestore/security/rules-conditions
但是,它没有按预期工作。
service cloud.firestore {
match /databases/{database}/documents {
match /articles/{articleId} {
allow read: if resource.data.status == 'published';
}
}
}
<小时/>
我使用以下 React + Redux 代码来查询文档:
export const getArticles = (limit = 25) => {
return (dispatch) => {
dispatch({ type: ARTICLES });
const db = firebase.firestore();
const query = db.collection('articles')
.orderBy('createdAt', 'desc')
.limit(limit);
query.get()
.then((snapshot) => {
const dataArray = [];
snapshot.forEach(doc => {
dataArray.push(mergeDocIdAndData(doc.id, doc.data()));
});
getArticlesSuccess(dispatch, dataArray)
.then(() => dispatch(push('/articles')));
})
.catch(() => getArticlesFail(dispatch));
};
}
最佳答案
您错过了文档中的一个特定点:您的查询失败“因为它不包含与您的安全规则相同的约束”和“安全规则不是过滤器”。请参阅https://firebase.google.com/docs/firestore/security/rules-query#queries_and_security_rules
换句话说,您的查询应该是这样的:
const query = db.collection('articles').where("status", "==", "published")...
关于javascript - 基于属性值的 Firebase Firestore 安全规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50526787/