My aim is to prevent authenticated users from voting more than once. I am using angularfire2. The code gives me the expected behavior and I was wondering the better way of doing this. The rateStatus(status: Status) function works, but is there any better way I would or should write this?


export class StatusService {
urlService: 'eStatus';
user: {};
private isloggedIn = new BehaviorSubject(false);
statusList: FirebaseListObservable<any[]>;
    private storeHelper: StoreHelper,
    private af: AngularFire,
    private _http: Http
  ) {

    this.af.auth.subscribe(user => {
       if(user) {
           this._uid = user.uid;
           this._username = user.auth.displayName;

    this.statusList = this.af.database.list('eStatus',  {
      query: {
        orderByChild: 'createdAt'


  rateStatus(status: Status) {
    this.path = this.af.database.object(`eStatus/${status.sid}`);
    let rStatusUser = this.af.database.object(`eRatingUsers/${status.sid}`);
    rStatusUser.subscribe(value => {
      if(value.uid == this._uid){
        console.log("already voted");
      } else {
         return rStatusUser.set({
          uid: this._uid
        .then(_ =>  this.path.update({ rating: status.rating + 1 }))
        .catch(err => console.log(err, 'You dont have access!')); 




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.