0

Sorry for my question which is probably simple but I didn't find the solution of my problem. I need to get one information from Firebase and the below function works well -> I've the console loging empty or not empty accordingly to the situation but I don't find a way to obtain the results elsewhere. It's probably cause I don't know how to use a Promise but ... I don't find the correct way to retrieve the boolean. Here is the function :

async checkIfCreatorUuidHasGameUuid(creatorUuid: string, showModeration: boolean, gameUuid : string): Promise<boolean> {
    try {
        let snap: firebase.firestore.QuerySnapshot;
        if (showModeration) {
            snap = await this.firestoreService.db.collection('announces').where('creatorUuid', '==', creatorUuid).where('gameUuid', '==', gameUuid).get();
        } else {
            snap = await this.firestoreService.db.collection('announces')
                .where('creatorUuid', '==', creatorUuid).where('inModeration', '==', false).where('gameUuid', '==', gameUuid).get();
        }

        if(snap.empty){
            console.log("empty")
            return false;
        } else {
            console.log("not empty")
            return true;
    }

    } catch (e) {
        console.error(e);
    }
}

Here is the call to this function :

if (this.announceService.checkIfCreatorUuidHasGameUuid(activeUser.uuid, true, this.game.uuid)) {
            console.log("true")
        }

It always log true and the promise returned by my previous function is always the same wether it's logging "empty" or "not empty" in the function above. What did I do wrong ? The function and the call to the function are in two separates pages for information Thanks

  • `if (await this.announceService.checkIfCreatorUuidHasGameUuid` – Liam Mar 24 '21 at 10:27
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Liam Mar 24 '21 at 10:27
  • Thanks ! It worked (and I knew I was missing something easy... Thank for your help) – Robin Rousset Mar 24 '21 at 18:10

1 Answers1

0

Its always logging true because you are calling the async function without awaiting for the response.

That's why you will always receive true, if you want the correct value for the Promise< boolean > return, just do:

if (await this.announceService.checkIfCreatorUuidHasGameUuid(activeUser.uuid, true, this.game.uuid)) {
   console.log("true")
}