gpt4 book ai didi

angular - 注销时如何摆脱 "Error: permission_denied"? (Angular-Firebase)

转载 作者:行者123 更新时间:2023-12-04 09:42:03 27 4
gpt4 key购买 nike

嗨,我是 Angular 和 Firebase 的新手,对下一个问题感到困惑:
我有两个组成部分-
授权区域(父组件)和文件夹(子组件)。授权区域有一个注销按钮,文件夹可以访问来自 firebase 的 auth-data。问题是,当用户注销所有操作成功时,会出现一条错误消息,我正试图摆脱它。
父组件:authorized-zone

export class AuthorizedZoneComponent implements OnInit {

constructor(public authService: AuthService) { }
ngOnInit(): void {}

//btn logout listener
logout() { this.authService.logout(); }
}
   <app-folders></app-folders>
<button id="logout"(click)="logout()">logout</button>`


子组件:文件夹

export interface Folder{
id:string;
name:string;
key: string;
}

//This Component is to be used only by Authorized-zone!
export class FoldersComponent implements OnInit {

private dbPath='/folders';//path to folders in realtime data base
private folderArray: Array<Folder>=[];//Array for folders: each folder has id and name

constructor(private router: Router, private route:ActivatedRoute, private db: AngularFireDatabase) {

this.db.list(this.dbPath).valueChanges().subscribe(data => {
for( let i=0; i< data.length; i++){
this.folderArray.push({id: data[i]["id"], name: data[i]["name"], key: data[i]["key"]});
}
})
}

ngOnInit(): void {}

//returns array of folders
getFolders(){ return this.folderArray;}

//on folder click listerner
onSelect(folder){}
}
<div (click)="onSelect(folder)" *ngFor="let folder of getFolders()> 
<div class="folder" [id]="folder.id">{{folder.name}}</div>
</div>


auth-service 使用户能够登录和注销

事件流似乎工作正常。
但是,当我按下注销按钮时,虽然功能有效,但我收到一条控制台错误消息:
ERROR Error: permission_denied at /folders: Client doesn't have permission to access the desired data...

我注意到如果我删除 <app-folders></app-folders>错误不准确。

我试过删除 <app-folders></app-folders>在authorized-zone.component 中注销之前:
//btn logout listener
logout() {
document.querySelector('app-folders').replaceWith("");
this.authService.logout();
}

它没有用,如果有人能够解释问题并推荐解决方案,我将非常感激

最佳答案

我找到了解决方案:在文件夹组件中使用 ngOnDestroy。


private dbData;

constructor(private router: Router, private route:ActivatedRoute, private db: AngularFireDatabase) {

this.dbData=this.db.list(this.dbPath).valueChanges()
.subscribe(data => {//...


})
}


//destroy firebaseData before logging out
ngOnDestroy(){
this.dbData.unsubscribe();
}

关于angular - 注销时如何摆脱 "Error: permission_denied"? (Angular-Firebase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62283995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com