angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Just get the value once with no subscription

Open bradintheusa opened this issue 10 years ago • 31 comments

Hi

I want just a snapshot of a node and not subscribe to updates. What is the best way to do this? I tried

var team = this.af.database.object('/teams/' , { preserveSnapshot: true }).first();
team.subscribe(snapshot => {
     // code here....
}

But i get a compile error on the call to first{}

store.ts (187, 92): Property 'first' does not exist on type 'FirebaseObjectObservable<any>'.

What is the best way to no receive updates.

Thanks Brad

bradintheusa avatar Aug 20 '16 03:08 bradintheusa

Yeah this works great. I did everything similiar but when I log out I get FIREBASE WARNING: Exception was thrown by user callback. Error: permission_denied at /users/id: Client doesn't have permission to access the desired data. warning in my console. It's also not "kicking"the user back to the login page.

What happens when you click log out in your app?

theunreal avatar Oct 18 '16 12:10 theunreal

If you click logout, you are redirected to the login page. I don't get the error, check the repo. The error you get seems to be a firebase rule error. You might have the read/write blocked while the user is not logged in and when you are making the logout, you probably logout successfully and after that you try to do some other operation on the firebase which will throw this error. Just to prove that what i said is right i have made an example with "rules": { ".read": "auth != null", ".write": "auth != null",

}

in my firebase rules and than i tried to make a read from firebase. I get the exact same error as you do.

main.4cd3da4….bundle.js:29 Error: permission_denied at /items: Client doesn't have permission to access the desired data. at Error (native) at G (http://sunmool.umbrellasoftware.ro/main.4cd3da4e9edd4e76637f.bundle.js:50:20875) at Object.G (http://sunmool.umbrellasoftware.ro/main.4cd3da4e9edd4e76637f.bundle.js:50:23045) at http://sunmool.umbrellasoftware.ro/main.4cd3da4e9edd4e76637f.bundle.js:50:16137 at nh.h.wd (http://sunmool.umbrellasoftware.ro/main.4cd3da4e9edd4e76637f.bundle.js:52:10092) at af.wd (http://sunmool.umbrellasoftware.ro/main.4cd3da4e9edd4e76637f.bundle.js:51:15299) at vd.hg (http://sunmool.umbrellasoftware.ro/main.4cd3da4e9edd4e76637f.bundle.js:49:28617) at yd (http://sunmool.umbrellasoftware.ro/main.4cd3da4e9edd4e76637f.bundle.js:49:15973) at bound WrappedWebSocket.La.onmessage (http://sunmool.umbrellasoftware.ro/main.4cd3da4e9edd4e76637f.bundle.js:51:3445) at bound WrappedWebSocket.t as _onmessage

andreigiura avatar Oct 18 '16 12:10 andreigiura

Further from this i can only pinpoint your problem if i get your source code

andreigiura avatar Oct 18 '16 12:10 andreigiura

@andreigiura Your rules have security risk. Other users can read and write data on other users! The rules have to be:

"users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    },

Change it and you will get the same error as I do. But no longer! Solved it by changing my logout method:

logout() {
      this.userSub.unsubscribe();
      this.auth.logout();
      this.router.navigate[''];
  }

I had to unsubscribe from the subscription in order to stop receiving updates which caused this error.

theunreal avatar Oct 18 '16 12:10 theunreal

I am aware about the security risk for my security rules and it was not a recommandation to turn your security off. I just said that you should review the whole code and make sure that you are not doing any other operations on firebase after you logged out.

andreigiura avatar Oct 18 '16 12:10 andreigiura

Thanks @andreigiura your code worked for me.. https://github.com/angular/angularfire2/issues/456#issuecomment-254464619 here i added a specific user as admin like this, ` //-----

    var isSuper = false;
    var isSuperEmail = "*******@gmail.com";
    if(email == isSuperEmail){
        isSuper = true;
    }`

and in dataToSet, `//-----

     var dataToSet = {
      timestamp : Math.floor((new Date()).getTime() / 1000),
      displayName : this.userData.auth.providerData[0].displayName,
      email : this.userData.auth.providerData[0].email,
      photoURL : this.userData.auth.providerData[0].photoURL,
      providerId : this.userData.auth.providerData[0].providerId,
      registeredAt : "",
      isSuper : isSuper
    };`

it works fine... but i am not sure writing admin email in component.ts file is safe or not?? is there any other way to do??

hareeshmh avatar Jan 24 '17 18:01 hareeshmh

@andreigiura In order to make sure you dont get the client permission on logout, you have to make sure to unsubscribe from every database query subscription you made. That is, for every this.af.database.list.subscribe or this.af.database.object.subscribe, you gotta make sure you implement ngOnDestroy method in that component where you subscribed. Inside ngOnDestroy() you gotta unsubscribe them. Dont try to unsubscribe from this.af.auth upon logout. this.af.auth.subscribe does not create client permission error, only "this.af.database" ones do.

For example, look at the following codes below from one of my angular2 firebase projects:

import { Component, OnInit, OnDestroy } from '@angular/core'; import { FirebaseService } from '../../services/firebase.service';

@Component({ selector: 'app-listings', templateUrl: './listings.component.html', styleUrls: ['./listings.component.css'] }) export class ListingsComponent implements OnInit, OnDestroy { listings:any; private subscription;

constructor(public firebaseService:FirebaseService) { }

ngOnInit() { this.subscription = this.firebaseService.getListings().subscribe(listings => { this.listings = listings; }); }

ngOnDestroy() { this.subscription.unsubscribe(); }

}

zeerulz90 avatar Apr 03 '17 23:04 zeerulz90

@andreigiura

Code below for origin of getListings() called:

constructor(public af: AngularFire, public router : Router) { this.folder = 'listingImages'; this.listings = this.af.database.list('/listings') as FirebaseListObservable<Listing[]> ............................. }

getListings () { return this.listings; }


Hope that helps

zeerulz90 avatar Apr 03 '17 23:04 zeerulz90

@zeerulz90 do you have a git / plunker repository where this subscription mechanism is used? I would like to have a closer look at it. Thanks )

neo-split avatar Apr 28 '17 21:04 neo-split

When to get once a collection like that

var team = this.af.database.object('/teams/' , { preserveSnapshot: true }).take(1);

How to refresh the collection after added an item ?

Anthony2539 avatar Oct 28 '17 19:10 Anthony2539

FirebaseListObservable is updated to angularfireobject, how can i use take??? Plz help, its showing take is not a property

noor07 avatar Mar 23 '18 13:03 noor07