Angular with Firebase can't access data

2019-08-13 08:06发布

问题:

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.