firebaseionic-react

ionic react doing firebase query


I am using ionic react and stuck on query data. i know how to update specific data using code like below:

const dbref = firebase.database().ref('/') 
await dbref.child("/withdrawQueue").push({"date": (new Date()).getTime(), "uid": uid, "coins": diviCoins, "status": "pending"})

as it is a ref so that does not work to do data query or at least i dont know how. i did something like

dbref.child("withdrawQueue").orderByChild("uid").equalTo(uid).once('value', resp=> {
          console.log("user withdraw request is", resp)
}

but this is not coming to the resp => i have no idea why.


Solution

  • It's hard to say for sure what's happening here, but I'll give you some pointers to getting to the root of the issue.

    Setting the data according to firebase

    According to the firebase/database doc (version 8, which looks like you're using), this is the way to update data:

    const childRef = ref.child('users');
    const elemRef = childRef.push(uid);
    elemRef.set({
        "date": (new Date()).getTime(),
        "coins": diviCoins,
        "status": "pending"
    },(err) => {
        console.log(err) // make sure your set isn't erroring.
    })
    

    Push also accepts an error handler, and that's a good diagnostic. Push also accepts this error function. If you really need to set the UID you probably have to use push. I just tend to take their given UIDs.

    You can also inspect the contents of the database from the firebase console to see if the data is being written.

    If the data exists in the database

    This could also be erroring on the call if the authentication isn't set up correctly. If you're using Admin APIs you'll need a service account, and pass the service account credentials along to the app to get access. Otherwise, make sure the authentication is working first, then check the database rules to make sure the user is authorized to access that data. If you're in testing mode, that's probably not an issue, but you'll need to check that as well.

    Assuming authentication is set up

    Passing the error function will help diagnose errors, also try without the ordering/filtering. My guess is that this call is not succeeding, and the error might help you determine why.

    dbref.child("withdrawQueue").once('value', resp => {
        console.log("user withdraw request is", resp)
    },(err)=>{
       console.log(err)
    })
    

    Check the dev-tools/console

    The browser's dev-tools console often gives you more information about what's going on. Check there for additional errors.