我正在尝试创建一个文档引用,设置一个onsnapshot监听器,保存文档,然后上传一个文件,该文件将触发一个云函数,该函数将对我正在收听的文档进行更新。但是在快照运行一次(我猜是初始状态)之后,onSnapshot
会给出一个权限错误'FirebaseError: Missing or insufficient permissions.'
。
我已经尝试在firebase控制台中运行访问和写入数据的模拟,并且运行正常,没有任何错误
const db = window.firebase.firestore()
const newBaseRef = db.collection('base').doc()
newBaseRef.onSnapshot(doc => {
console.log('Current data: ', doc.data())
}, function (error) {
throw error // THIS ALWAYS GETS HIT
})
newBaseRef.set({
uid: window.firebase.auth().currentUser.uid,
createdAt: window.firebase.firestore.FieldValue.serverTimestamp()
})
以下是我的安全规则
service cloud.firestore {
match /databases/{database}/documents {
match /printset/{document=**} {
allow read, update, delete: if request.auth.uid == resource.data.uid
allow create: if request.auth.uid != null;
}
match /file/{document=**} {
allow read, update, delete: if request.auth.uid == resource.data.uid
allow create: if request.auth.uid != null;
}
match /base/{document=**} {
allow read, update, delete: if request.auth.uid == resource.data.uid
allow create: if request.auth.uid != null;
}
}
}
我不期望运行错误回调
发布于 2019-05-10 08:33:28
newBaseRef.set()
返回Promise
。
因此,当调用newBaseRef.onSnapshot()
时,newBaseRef.data().uid
还没有设置。
请参见:
应该在Promise.resolve()
之后调用newBaseRef.onSnapshot()
。
const db = window.firebase.firestore()
const newBaseRef = db.collection('base').doc()
newBaseRef.set({
uid: window.firebase.auth().currentUser.uid,
createdAt: window.firebase.firestore.FieldValue.serverTimestamp()
}).then(() => {
newBaseRef.onSnapshot(doc => {
console.log('Current data: ', doc.data())
}, function (error) {
throw error // THIS ALWAYS GETS HIT
})
})
还有更多。
如果你只想插入,那么你应该使用newBaseRef.add({})
。
如果你想插入或替换所有数据,那么你应该使用newBaseRef.set({})
。
如果你想更新或者更新,那么你应该使用newBaseRef.set({}, {merge, true})
。
如果你只想更新,那么你应该使用newBaseRef.update({})
。
如果您想要更新或更新,则将您的安全规则更改为以下设置。
service cloud.firestore {
match /databases/{database}/documents {
match /printset/{document=**} {
allow read, update, delete: if request.auth.uid == resource.data.uid
allow create: if request.auth.uid != null;
}
match /file/{document=**} {
allow read, update, delete: if request.auth.uid == resource.data.uid
allow create: if request.auth.uid != null;
}
match /base/{document=**} {
allow read, delete: if request.auth.uid == resource.data.uid
allow update: if resource == null || request.auth.uid == resource.data.uid
allow create: if request.auth.uid != null;
}
}
}
https://stackoverflow.com/questions/56065707
复制相似问题