Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase's SET method isn't working

I'm using firebase and I wrote a simple function in Javascript to SET and UPDATE data.

function setLastOnline(userID, InOut){
    var dbLastonline = firebase.database().ref().child("lastonline");
    if (InOut) {
        dbLastonline.child(userID).set({
            "isConnected":true,
            "connectedby":"web",
            "lastIn":firebase.database.ServerValue.TIMESTAMP,
            "lastOut":0
        });
    } else {
        dbLastonline.child(userID).update({
            "isConnected":false,
            "connectedby":"web",
            "lastOut":firebase.database.ServerValue.TIMESTAMP
        });
    }
}

When I call the function to perform the SET method, nothing happens... No data in database and no error code...

    firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            setLastOnline(user.uid, true);
        }
      }

But when I call the same function to perform the UPDATE, everything works fine.

    firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            setLastOnline(user.uid, false);
            firebase.auth().signOut();
        }`
     }`

What am I doing wrong?

Thanks in advance.

like image 591
Rogerio Setubal Avatar asked Nov 23 '25 23:11

Rogerio Setubal


1 Answers

I found the error. The above function is correct and works well. The problem was that I called a window.open() shortly after calling setLastOnline(). Like this:

firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            setLastOnline(user.uid, true);
            window.open('another_page.php','_self');
        }
      }

So the Firebase's execution stops and the VERBOSE in Chrome console ([Violation] Avoid using document.write() at line 362 of www.gstatic.com/firebasejs/3.6.8/firebase.js) appears.

Regards!

like image 81
Rogerio Setubal Avatar answered Nov 26 '25 12:11

Rogerio Setubal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!