【问题标题】:retrieve all data by all users in firebase检索firebase中所有用户的所有数据
【发布时间】: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


【解决方案1】:

如果要允许所有用户阅读所有用户的所有帖子,则需要授予他们访问整个/report 节点的权限。在您的安全规则中,您可以通过将".read": true 规则上移一级来做到这一点:

{
  "rules": {
    "report": {
      ".read": true,
      "$uid": {
        ".write": "$uid === auth.uid"
      }
    }    
  }
}

但这意味着您还需要更新您的代码以收听所有/report,然后遍历其下的各个用户。

【讨论】:

  • 同样的问题,我只能红我自己的帖子。 l 可以阅读其他用户的帖子!
  • 是的,我建议只尝试在 root 上读取以确认它是否是规则问题,但这并没有帮助,所以我猜这是一个代码问题
【解决方案2】:

正如我所说,我不擅长 angularfire,但是这条线

this.itemsRef = this.db.list(`report/${data.uid}`);

对我来说似乎是为仅登录的用户提取数据, 我觉得可能需要

this.itemsRef = this.db.list(`report`);

然后遍历检索到的快照中的所有元素

更新

尝试将".read": true, 移到$uid 上方,在reports 下方, 从另一个答案中复制确切的规则

【讨论】:

  • 如果我使用你的代码,我得到了空的数据 html。我必须使用report/${data.uid} 这个来获取数据,但是我只有一个数据用户没有循环所有数据
  • 抱歉有错误,应该重新报告,刚刚更正了
  • 如果我使用this.itemsRef = this.db.list(report);这个代码我得到了空数据html!
  • 复制其他答案的规则
  • 天哪,我不知道为什么我只收到我的帖子,我没有收到其他用户的帖子! ,您可以使用 timeviewr 访问我的代码来查看吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 2021-07-28
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多