簡體   English   中英

如何在沒有Node.js的情況下使用Firebase實時數據庫

[英]How to use firebase realtime database without node.js

我是Firebase Realtime Database的新手,我想在我的項目中實現Firebase Realtime數據庫,但想要不使用node.js來實現。 可能嗎 ? 請幫助我解決這個問題。

如果可以在不使用node.js的情況下使用firebase實時數據庫,那么我該如何進一步。 我還將示例代碼放在下面。 請給我一些建議。

var config = {
apiKey: "AIzaSyAEERy5Jbnns9CUurD7NwmOXXXXXXXXXX",
authDomain: "testing-XXXXX-db.firebaseapp.com",
databaseURL: "https://testing-XXXXX-db.firebaseio.com",
projectId: "testing-XXXXXX-db",
storageBucket: "testing-XXXXX-db.appspot.com",
messagingSenderId: "25861013XXXX"
};
firebase.initializeApp(config);

// Get a reference to the database service
var database = firebase.database();

我已經成功初始化它,而且我還從雲數據庫中獲得結果。

firebase.database().ref("/chat").orderByChild("name").on("value", function(snapshot) {
      snapshot.forEach(function(childSnapshot) {

    // key
    var key = childSnapshot.key;
    // value, could be object
    var childData = childSnapshot.val();

    console.log(childData);
    $("#history").append('<div class="container"><p>'+childData.name+':'+childData.message+'</p><span class="time"> Time : '+childData.time+' </span></div>');
  });
});

當我想使用數據庫觸發器進行聊天應用程序的實時更新時,問題就來了。

我想使用以下事件/功能。

onWrite(), which triggers when data is created, destroyed, or changed in the Realtime Database.
onCreate(), which triggers when new data is created in the Realtime Database.
onUpdate(), which triggers when data is updated in the Realtime Database.
onDelete(), which triggers when data is deleted from the Realtime Database.

任何幫助都非常感謝。 不好的語法/語言的歉意。 請不要給予負面評價,因為我真的很堅持。

我對Firebase的了解不是最多,但是據我了解,您列出的那些功能是通過使用Cloud Functions觸發的,這些功能不需要響應瀏覽器中的更改。

在瀏覽器中,要響應子項更改,請使用child_changed

 firebase.database().ref("chat").orderByChild("name").on("child_changed", function(snapshot) {
     //-- Whatever.
 });

要響應添加子項,請使用child_added

 firebase.database().ref("chat").orderByChild("name").on("child_added",      function(snapshot) {
     //-- Whatever.
 });

要響應對子項的刪除,請使用child_removed

 firebase.database().ref("chat").orderByChild("name").on("child_removed", function(snapshot) {
          //-- Whatever.
 });

另外,請注意,如果您對任何內容使用.on(“ value”),則每次數據以任何方式更改時都會返回一個新的數據集,這意味着這可能會在每次收到新消息時返回ent chat發送,而不僅僅是返回新消息。 可能不想這樣做:

 firebase.database().ref("chat").orderByChild("name").on("value", function (){});

如果您只想獲取一次聊天數據,請使用.once("value")

我希望這有幫助?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM