Ionic AngularFireDatabase 说没有授权,即使规则是开放的
Ionic AngularFireDatabase saying not authorized even though rules are open
概览
我正在尝试从 firebase 数据库获取数据,但它说我没有权限,即使规则是完全开放的。
我按照 this youtube tutorial. After running into errors with the deprecated FirebaseListObservable, I changed my provider according to this update information. I also tried using the default open Firebase rules as well as the ones they recommend for "Allow All" in the Firebase documentation.
开始构建当前的实现
提供商
错误信息
Firebase 规则
规则的默认开放版本,也不起作用,有行 allow read, write;
数据库
其他可能相关的信息
与 1 minute into the mentioned youtube tutorial 相同,我在 app.module.ts 的导入中调用 initializeApp(firebaseConfig)
使用单击 "Add Firebase to your web app"[=24 的信息=]
我正在使用 angularfire2@5.0.0-rc.8.0
,我是用 npm install angularfire2
安装的。刚才我运行 npm list
的时候它给了我下面两个warning,可能跟问题有关。我正在使用 @angular/common@5.2.10
和 @angular/core@5.2.10
npm ERR! peer dep missing: @angular/common@^6.0.0, required by angularfire2@5.0.0-rc.8.0
npm ERR! peer dep missing: @angular/core@^6.0.0, required by angularfire2@5.0.0-rc.8.0
解决方案
问题是在 Firebase 仪表板中我配置的是 Cloud Firestore (BETA)
数据库而不是 Realtime Database
数据库。切换到使用正确的数据库并设置规则后,我能够使用基本的 firebase 库(不需要 angularfire2)从存储和实时数据库获取数据。
工作代码示例
在这个例子中,我获取了根节点的快照,然后基于该节点的键,遍历每个子节点的快照并将 .val()
存储到关联的数组中。 (注意:新数据库中的数据布局与问题截图中的格式不同)
import * as firebase from 'firebase';
...
guardiansList: any = [];
warriorsList: any = [];
prayersList: any = [];
...
try {
firebase.database().ref().once('value', (snapshot) => {
snapshot.forEach((categorySnap) => {
switch (categorySnap.key) {
case "guardians":
categorySnap.forEach((imgSnap) => {
this.guardiansList.push(imgSnap.val());
return false;
});
break;
case "warriors":
categorySnap.forEach((imgSnap) => {
this.warriorsList.push(imgSnap.val());
return false;
});
break;
case "prayers":
categorySnap.forEach((imgSnap) => {
this.prayersList.push(imgSnap.val());
return false;
});
break;
default:
console.log("Found Unknown Key");
break;
}
return false;
});
});
}
catch (e) {
console.log(e);
}
正如@brenthompson2 提到的那样,我分享了使用 Firestore(beta) 的方法
在app.module.ts
import { AngularFirestoreModule } from 'angularfire2/firestore';
...
@ngModule({
...
imports: [
...
AngularFireModule.initializeApp(environment),
AngularFirestoreModule,
],
})
并且在任何 angular 模块或注射剂中
constructor(private store:AngularFirestore) {
}
...
this.store.doc<any>(`users/${uid}`).snapshotChanges().subscribe(
data=>{console.log(data.payload.data())}
);
与 firebase2/database
没什么不同
概览
我正在尝试从 firebase 数据库获取数据,但它说我没有权限,即使规则是完全开放的。
我按照 this youtube tutorial. After running into errors with the deprecated FirebaseListObservable, I changed my provider according to this update information. I also tried using the default open Firebase rules as well as the ones they recommend for "Allow All" in the Firebase documentation.
开始构建当前的实现提供商
错误信息
Firebase 规则
规则的默认开放版本,也不起作用,有行 allow read, write;
数据库
其他可能相关的信息
与 1 minute into the mentioned youtube tutorial 相同,我在 app.module.ts 的导入中调用 initializeApp(firebaseConfig)
使用单击 "Add Firebase to your web app"[=24 的信息=]
我正在使用 angularfire2@5.0.0-rc.8.0
,我是用 npm install angularfire2
安装的。刚才我运行 npm list
的时候它给了我下面两个warning,可能跟问题有关。我正在使用 @angular/common@5.2.10
和 @angular/core@5.2.10
npm ERR! peer dep missing: @angular/common@^6.0.0, required by angularfire2@5.0.0-rc.8.0 npm ERR! peer dep missing: @angular/core@^6.0.0, required by angularfire2@5.0.0-rc.8.0
解决方案
问题是在 Firebase 仪表板中我配置的是 Cloud Firestore (BETA)
数据库而不是 Realtime Database
数据库。切换到使用正确的数据库并设置规则后,我能够使用基本的 firebase 库(不需要 angularfire2)从存储和实时数据库获取数据。
工作代码示例
在这个例子中,我获取了根节点的快照,然后基于该节点的键,遍历每个子节点的快照并将 .val()
存储到关联的数组中。 (注意:新数据库中的数据布局与问题截图中的格式不同)
import * as firebase from 'firebase';
...
guardiansList: any = [];
warriorsList: any = [];
prayersList: any = [];
...
try {
firebase.database().ref().once('value', (snapshot) => {
snapshot.forEach((categorySnap) => {
switch (categorySnap.key) {
case "guardians":
categorySnap.forEach((imgSnap) => {
this.guardiansList.push(imgSnap.val());
return false;
});
break;
case "warriors":
categorySnap.forEach((imgSnap) => {
this.warriorsList.push(imgSnap.val());
return false;
});
break;
case "prayers":
categorySnap.forEach((imgSnap) => {
this.prayersList.push(imgSnap.val());
return false;
});
break;
default:
console.log("Found Unknown Key");
break;
}
return false;
});
});
}
catch (e) {
console.log(e);
}
正如@brenthompson2 提到的那样,我分享了使用 Firestore(beta) 的方法
在app.module.ts
import { AngularFirestoreModule } from 'angularfire2/firestore';
...
@ngModule({
...
imports: [
...
AngularFireModule.initializeApp(environment),
AngularFirestoreModule,
],
})
并且在任何 angular 模块或注射剂中
constructor(private store:AngularFirestore) {
}
...
this.store.doc<any>(`users/${uid}`).snapshotChanges().subscribe(
data=>{console.log(data.payload.data())}
);
与 firebase2/database
没什么不同