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.