2017-05-29 85 views
3

我正在使用firebase并且我在Javascript中编写了一个简单函数来设置和更新数据。Firebase的SET方法不起作用

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 
     }); 
    } 
} 

当我打电话要执行的功能设置方法,什么也没有发生......在数据库中无误码无数据...

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

但是,当我调用同一个函数来执行更新,一切工作正常。

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

我在做什么错了?

在此先感谢。

+0

我不会立即看到这段代码有什么问题。你能创建一个重现问题的jsbin吗? –

+0

在** set **调用之前添加一个打印,以确保您的代码正在按预期运行... –

+0

我已经测试了一切。该代码正在执行,但不记录在数据库中。在今天的测试中,我在Chrome控制台中发现了Firebase的VERBOSE:** [违规]避免在www.gstatic.com/firebasejs/3.6.8/firebase.js**的第362行使用document.write()。它出现在执行SET方法的恰好时刻。 –

回答

1

我发现了错误。上述功能是正确的,并且运作良好。问题是我在调用setLastOnline()之后不久调用了window.open()。就像这样:

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

所以火力地堡的停止执行和VERBOSE在Chrome控制台([违反]避免在www.gstatic.com/firebasejs/3.6.8/firebase的362线使用document.write() .js)出现。

问候!

+1

好抓!细节决定成败... ;) –