How to use Promise in Firebase Query?

Asked

Viewed 88 times

0

I need to do basic languages like this:

1) You will query Firebase, checking if there is a certain ID. If there is this ID, it will give . then saying that it is ok and do something, if it will not give . catch, stating the problem.

What I tried to do was this: But I don’t think I did it right.

 this.queryMAP = () => {
        //let userId = this.getDBSReference().currentUser.uid;
        let mapRef = this.getDBSReference().ref('users/Aliansce/LOCATIONS/').orderByKey().equalTo('map1');
        mapRef.on('child_added', (snap) => {
            console.log(snap.key)
            snap.key.this.createPromise()
    });

    this.createPromise = () => {
        let promise = new Promise((resolve, reject) => {
            console.log(snap.key)
            if(snap.key == "map1"){
                resolve("igual")
            } else {
                reject("diferente");
            }
        }).then((sucess) =>{
            console.info("ae");
        }).catch((err) => {
            console.warn("error")
        });
    };
  • I think .orderByKey().equalTo('map1') is not the correct syntax not expensive. But from what I realized you want to find the object with the key map1. I’m posting my answer now

1 answer

0

From what I noticed, you want a location via your key (id). I suggest you use the function once(). According to the documentation the once() returns a Promise.

Then you would:

this.queryMAP = () => {
        //let userId = this.getDBSReference().currentUser.uid;
        let mapRef = this.getDBSReference().ref('users/Aliansce/LOCATIONS/').child('map1');
        mapRef.once('value').then((snap) =>{
            console.log(snap.key);
            console.info("ae");
        }).catch((err) => {
            console.warn("error");
        });
    };

Browser other questions tagged

You are not signed in. Login or sign up in order to post.