我得到了相当常见的Uncaught (in promise) FirebaseError: Missing or insufficient permissions.
错误。通常这是因为你没有改变你的安全规则,即它们太严格了。不过,就我而言,我尝试了一些不同的规则集,自由主义程度越来越高:
这是我最初的规则,我可以理解为什么可能只有当你登录时才有效:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId;
allow update: if request.auth.uid == userId;
}
}
}
read
规则随后变为(其他一切保持不变(:
allow read: if true;
然后变成:
allow read;
每次,我仍然会犯同样的错误。
作为参考,以下是我打电话的方式:
App.js
const app = firebase.initializeApp(firebaseConfig); // copied from SDK snippet
// then, inside the component:
useEffect(() => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
setLoggedIn(true);
}
});
getTeas(app);
});
firebase.js
import * as firebase from "firebase";
export function getTeas(app) {
const db = firebase.firestore(app);
db.collection("teas")
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
});
});
}
您的规则授予访问/users
集合的权限。无论您指定什么条件(if
之后的部分(,它都只适用于访问users
集合。
您的代码正在尝试从/teas
集合中读取。由于没有声明授予对/teas
访问权限的规则,因此该操作被拒绝。
如果也希望授予对/teas
的访问权限,则需要为此添加一个附加规则,如Firebase关于构建Firestore安全规则的文档所示。