我在项目中使用 Firebase + Ionic。注销时出现我的问题。我订阅了一些集合中的多个 onSnapshot
事件。我希望只要用户注销,所有订阅都会被取消,但事实并非如此,所以每当我注销时,我都会收到来自 Firebase 的几个错误:
Uncaught Error in onSnapshot: Error: Missing or insufficient permissions.
这是我的代码:
我的 Controller
/**
* Logout button 'click' event handler
*/
onLogoutPressed(){
this.fbAuthServ.logout().then(() => {
this.navCtrl.setRoot(LoginPage);
});
}
我的服务提供商
// Firebase Modules
import { AngularFireAuth } from 'angularfire2/auth';
constructor(private afAuth: AngularFireAuth, private utils: UtilsProvider){}
...
/**
* Firebase Logout the current user
*/
async logout(){
this.afAuth.auth.signOut();
}
请问我应该怎么做才能避免那些Missing or insufficient permissions
错误?
提前致谢!
编辑:我如何订阅 onSnapshot 事件
Controller
ionViewDidEnter(){
this.fbDataServ.getPlacesByUserAllowed().onSnapshot(placeReceived=> {
this.placesByUserAllowed = placeReceived.docs.map(placeSnapshot => {
return this.utils.mapPlaceSnapshot(placeSnapshot )
});
this._concatPlaces();
//Dismiss loading whenever we have data available
this.utils.dismissLoading();
});
服务提供商
// Firebase
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
constructor(private afs: AngularFirestore, private fbAuthServ: FirebaseAuthServiceProvider, private utils: UtilsProvider) { }
placesCollection: AngularFirestoreCollection<PlaceModel> = this.afs.collection("places");
/**
* Gets the collection of places where the user is 'allowed'
*/
public getPlacesByUserAllowed(){
return this.placesCollection.ref
.where('users.' + this.fbAuthServ.getCurrentUser().uid + '.allowed', '==', true);
}
最佳答案
由于错误消息提到了 onSnapshot
,我假设您正在代码中的某处访问 Firebase 数据库或 Cloud Firestore。
您从数据库中读取的数据配置了要求用户经过身份验证的安全规则。因此,当您注销用户时,不再满足该要求,应用程序失去对该数据的访问权限,并且观察者被取消。
为防止出现此错误,请在注销用户之前删除观察者。
更新
按照 Firestore documentation on detaching a listener 中显示的模式移除观察者/监听者。 .首先保留对从 onSnapshot
获得的返回值的引用。
var unsubscribe = this.fbDataServ.getPlacesByUserAllowed().onSnapshot(placeReceived=> {
然后在注销用户之前调用 unsubscribe()
,如下所示:
unsubscribe();
关于javascript - 注销后出现缺少或权限不足的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404498/