Wednesday, 23 January 2019

retrieve all data by all users in firebase

i am try to build blog app using firebase database use angularfire2 , l want to allowed to users can read all data post by others users , but unsuccessfully l dont know where is problem .

database rules !

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

      }
    }    

  }
}

When l use this rules the users does not read all posts for other user ! , only l can read my post .

main code

 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() }))
              )
            );


          }

        })

      }
}

database

{
  "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"
      }
    }
  }
}

if l use this code to retrieve data l got empty html data !

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

enter image description here

if l use this code to retrieve data l got only my own post not by others all users .

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



from retrieve all data by all users in firebase

No comments:

Post a Comment