【发布时间】:2019-01-21 08:35:49
【问题描述】:
我正在尝试使用firebase数据库使用angularfire2构建博客应用程序,我希望允许用户可以读取其他用户发布的所有数据,但不成功我不知道问题出在哪里。
数据库规则!
{
"rules": {
"report": {
"$uid": {
".read": "true",
".write": "$uid === auth.uid"
}
}
}
}
当我使用此规则时,用户不会阅读其他用户的所有帖子! ,只有我可以阅读我的帖子。
主要代码
export class FeedPage {
itemsRef: AngularFireList<any>;
items: Observable<any[]>;
constructor(public navCtrl: NavController, public navParams: NavParams, public fire: AngularFireAuth
,public alertCtrl: AlertController,public toastCtrl: ToastController,public popoverCtrl: PopoverController,
public db: AngularFireDatabase)
{
// // Use snapshotChanges().map() to store the key
// this.items = this.itemsRef.snapshotChanges().pipe(
// map(changes =>
// changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
// )
}
ionViewWillLoad(){
this.fire.authState.subscribe(data => {
if(data && data.email && data.uid){
this.toastCtrl.create({
message : ` welcome ${data.email}`,
duration:2000
}).present()
this.itemsRef = this.db.list(`report/`+data.uid);
// Use snapshotChanges().map() to store the key
this.items = this.itemsRef.snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
);
}
})
}
}
数据库
{
"report" : {
"8D3sENaBcLaXoGNnh1MPuoyj5LP2" : {
"-LWl294Hs6YjkvJE5pqi" : {
"post" : "ali",
"title" : "dd"
},
"-LWlEonKLWfOttzirqp7" : {
"post" : "sas",
"title" : "ass"
},
"-LWlGvn81Kes2A-1UcC2" : {
"post" : "asa",
"title" : "asass"
}
},
"WUM2HBkGo8TFDeOjEqO1s3lCj1p1" : {
"-LWlHlhyS9m3ECS3wIdk" : {
"post" : "qwqsasasa",
"title" : "as"
},
"-LWlHmXZAJdSPZurO7ii" : {
"post" : "qwqsasasa",
"title" : "as"
}
}
}
}
如果我使用这段代码来检索数据,我得到了空的 html 数据!
this.itemsRef = this.db.list(`report`);
如果我使用此代码检索数据,我只得到我自己的帖子,而不是其他所有用户的帖子。
this.itemsRef = this.db.list(`report/${data.uid}`);
【问题讨论】:
-
要确认您遇到的是规则问题而不是代码问题,请尝试删除您在 uid 上的规则,并在 root 上将 read 和 write 都设置为 true
-
是一样的! ,我做了你所说的我只得到一个用户而不是所有用户的帖子。我想得到所有用户的帖子!
-
那么问题出在你的代码而不是规则上。我不擅长 angularFire
-
请问您有什么解决办法吗?我厌倦了他
-
您的代码仍在从 UID 获取数据:
this.itemsRef = this.db.list(report/+data.uid);。因此,该列表比您尝试获取的报告列表和其他建议的列表低一个文件夹。
标签: javascript firebase firebase-realtime-database ionic3 angularfire2