Angular with Firebase can't access data

流过昼夜 提交于 2019-12-11 06:09:03

问题


I have a very simple app in Angular, that should read a small Firebase database...

As simple as:

import { Component } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})

export class AppComponent {
courses: any[];

constructor(db: AngularFireDatabase) {
db.list('/courses/IlZ5lAPfQDmWAf52hAI4')
  .valueChanges().subscribe(courses => {
    this.courses = courses;
    console.log(this.courses);
    });
  }
}

As it's for tests purpose, there should be no authentication to Firebase, in other words, anyone could read it.

The rules are set as:

service cloud.firestore {
match /{multi_path=*} {
match /{multi_path=**} {
  allow read, write
    }
  }
}

I should mention that in the Firebase simulator, I can list the/courses/IlZ5lAPfQDmWAf52hAI4 document.

But I keep getting the error of not having the permission, e.g.: RROR Error: permission_denied at /courses/IlZ5lAPfQDmWAf52hAI4: Client doesn't have permission to access the desired data.

I can't figure out what's going on... Should be simple..

Thanks


回答1:


You are using Firebase Real-Time database on your code, But you provide a Cloud Firestore rules.
You should either use Real-time database or Cloud Firestore. Go to your firebase console and under the section of Develop/Database you can switch between them. So, make your choice which one you want to use and provide its rules accordingly.
NOTE - currently you are using the Realtime database in your code, so if you are gonna use the cloud firestore, you have to change your imports in the module and use different objects. you should read more about it in the official documents.



来源:https://stackoverflow.com/questions/51540509/angular-with-firebase-cant-access-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!