[英]Uncaught Error in snapshot listener:, FirebaseError: [code=permission-denied]: Missing or insufficient permissions
I am trying to let the user only read and write their own data.我试图让用户只读写他们自己的数据。 My rules are as follow(from the docs)我的规则如下(来自文档)
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, update, delete: if request.auth != null && request.auth.uid == userId;
allow create: if request.auth != null;
}
}
}
The uid for my user matches my document id but i still get the error:我的用户的 uid 与我的文档 ID 匹配,但我仍然收到错误消息:
Uncaught Error in snapshot listener:快照侦听器中未捕获的错误:
FirebaseError: [code=permission-denied]: Missing or insufficient permissions. FirebaseError:[code=permission-denied]:权限缺失或不足。
My code for getting uid to document id我获取 uid 到文档 id 的代码
const handleSignUp = async () => {
auth
.createUserWithEmailAndPassword(email, password)
.then(async (UserCredentials) => {
const user = UserCredentials.user;
console.log("Registered with: ", user.email);
try {
const uidRef = doc(db, 'users', user.uid);
const docRef = await setDoc(uidRef, {
name: name,
age: age,
currentWeight: currentWeight,
goalWeight: goalWeight,
});
} catch (e) {
console.error("Error adding document: ", e);
}
I am really lost as I have tried many different ways and all docs / answers on here do not work for me.我真的迷路了,因为我尝试了很多不同的方法,这里的所有文档/答案都不适合我。 I am guessing the error comes when i call snapshot in this code我猜当我在此代码中调用快照时会出现错误
const getUser = async() => {
const subscriber = onSnapshot(usersRef, (snapshot) => {
let user = []
snapshot.docs.forEach((doc) => {
user.push({...doc.data(), key: doc.id })
})
setUser(user);
console.log(user);
})
return () => subscriber();
};
I am just unsure as to what is exactly wrong here.我只是不确定这里到底出了什么问题。 Is it my rules?这是我的规则吗? My snapshot?我的快照?
Given that you get a QuerySnapshot
result, I suspect that your code is reading the entire users
collection.鉴于您获得了QuerySnapshot
结果,我怀疑您的代码正在读取整个users
集合。 But as the documentation says rules are not filters , but instead merely ensure that your code only tries to access data that it is permitted to.但正如文档所说, 规则不是过滤器,而只是确保您的代码仅尝试访问允许的数据。
So your code should only try to read the document of the currently signed in user.所以你的代码应该只尝试读取当前登录用户的文档。
const getUser = async() => {
if (getAuth().currentUser) {
const uidRef = doc(db, 'users', getAuth().currentUser.uid);
const subscriber = onSnapshot(uidRef, (doc) => {
setUser({...doc.data(), key: doc.id })
})
...
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.