0

I have this method for checking if a firestore document exists:

rating_exists(userId : string, gameId :  string) : Boolean {
        let exists: boolean = false;
        const docRef = this.afs.collection('ratings').doc(`${userId}_${gameId}`).snapshotChanges()
        .subscribe(x => exists = x.payload.exists);
        return exists;

    }

but it seems that this method always returns false.

Its necessary for checking if a user has provided a rating for media. If the user has not provided a rating yet, it should create a rating document for that user

in rating component

    ngOnInit(): void {
        if(!this.starsService.rating_exists(this.userId, this.gameId)) {
            console.log("this happened- conditional dose not work!");
            this.starsService.setRating(this.userId, this.gameId, 0)
        }
        this.starsService.getGameRating(this.userId, this.gameId)
        .subscribe(doc => this.stars = doc);    
    }

I think that I'm misunderstanding how the scope of observable subscriptions work. console log message always prints despite the fact that document exists for the currently signed in user.

breadman0
  • 163
  • 1
  • 4
  • 14
  • you need to wait for subscription to finish then you can return. – salman Dec 09 '20 at 05:59
  • check this [link](https://stackoverflow.com/questions/38291783/how-to-return-value-from-function-which-has-observable-subscription-inside) – salman Dec 09 '20 at 06:21

0 Answers0