2016-12-25 127 views
2

如何使用Node.js使用Firebase正確設置值事件偵聽器?如何在Firebase值事件上設置偵聽器?

我讀通過Firebase's documents並已成功地能夠從我的兩個蘋果,我的樹莓派運行的同時,連接到同一個數據庫火力地堡上更新我的火力地堡數據庫表和行;但是,我不能明白我需要這個領域做製作一個數據事件的監聽器:

var starCountRef = firebase.database().ref('posts/' + postId + '/starCount'); 
starCountRef.on('value', function(snapshot) { 
    updateStarCount(postElement, snapshot.val()); 
}); 

這是明顯,我認爲我需要以某種方式需要根據定義postElement輸出:

FIREBASE WARNING: Exception was thrown by user callback. ReferenceError: postElement is not defined 

但是,我沒有想法是什麼postElement應該是,也不知道怎樣去定義它。幫幫我!

我做了一個非常小的程序和表來簡單地更新/更改值。我改變了程序,當它改變了一行時,我可以看到控制檯輸出。

這是我小的用戶數據變量,只是讓你我想現在該怎麼少的數據更改:

var user = { 
    id:Number, 
    name:String, 
    number:Number 
}; 

var users = [user]; 

這是我改變文檔的代碼,這樣我就可以看到東西而Node.js的控制檯輸出運行:

var userRef = firebase.database().ref('/users/user' + users[0].id); 
userRef.on('value', function(snapshot){ 
    firebase.app.updateUserInfo(postElement, snapshot.val()); 
    console.log('Users' + users[0].name + ' changed.');//<- this is the line I added. 
}); 

注:updateStarCount功能是從火力地堡文檔中的例子,有意義的例子有,我小心翼翼地承擔更新本地火力地堡分貝...

注意2:如果updateStarCount正在做一些其他的魔術,那麼它是Firebase的文檔缺陷,因爲函數沒有在那裏定義。任何幫助澄清本文檔將不勝感激。

+0

此'updateStarCount'函數來自哪裏?這絕對不是來自Firebase。 – vzsg

+0

@vzsg如果您檢查[Firebase頁面]的鏈接(https://firebase.google.com/docs/database/web/read-and-write)並搜索'updateStarCount',您可以在鏈接頁面。 – NonCreature0714

+0

啊,我明白了。我應該將其表述爲「不是來自Firebase SDK」。這只是示例代碼,並不比僞代碼更具體。這裏的想法假設你有一個函數用結果更新HTML元素 - 你必須用真實應用程序中的自己的邏輯來替換它。 – vzsg

回答

4

爲了讓其他人尋找到同樣的問題,這個問題有幫助,這裏是從缺失的函數和變量的問題定義的擴展的例子:

// some HTML element on the page 
var postElement = document.getElementById("postElement"); 

// here I will assume that this function simply 
// updates the contents of the element with a value 
var updateStarCount = function(element, value) { 
    element.textContent = value; 
}; 

var starCountRef = firebase.database().ref('posts/' + postId + '/starCount'); 
starCountRef.on('value', function(snapshot) { 
    updateStarCount(postElement, snapshot.val()); 
}); 

有了這個JavaScript,你可以假設HTML頁面看起來像這樣:

<html> 
    <body> 
    <div id="postElement"></div> 
    </body> 
</html> 
0

繼IgorNikolaev的例子......爲了讓這個帖子更有助於其在樹莓派的或作爲一個獨立的應用程序(不適用於網頁)與Node.js的同樣的問題的人。

感謝@vzsg,Firebase Admin SDK可能更適合我的情況。

步驟來創建一個完全工作的例子是...

這隻能寫入數據庫的封面,其結果可以在登錄到火力地堡的控制檯中可以看出。

首先,create Firebase admin authentication,則:

var admin = require("firebase-admin"); 
admin.initializeApp({ 
    credential:admin.credential.cert("/path/to/credential/cert.json"), 
    databaseURL: "databaseURLhere" 
}); 

var db = admin.database(); 
var ref = db.ref("/"); 
console.log('DB ref: ' + ref); 

var usersRef = ref.child("users"); 

使用set在數據庫中創造新的價值。 但請注意,set會覆蓋預先存在的值。

usersRef.set({ //With completion callback. 
    alanisawesome: { 
     date_of_birth: "June 23, 1912", 
     full_name: "Alan Turing" 
    }, 
    gracehop: { 
     date_of_birth: "December 9, 1906", 
     full_name: "Grace Hopper" 
    } 
    }, 
    function(error) { //NOTE: this completion has a bug, I need to fix. 
    if (error) { 
     console.log("Data could not be saved." + error); 
    } else { 
     console.log("Data saved successfully."); 
    } 
}); 

追加新的列(或更改當前數據)update排,但要格外小心給予路徑列,否則,所有以前的專欄被覆蓋,而不是附加表:

usersRef.update({ 
    "alanisawesome/nickname": "Alan The Machine", 
    "gracehop/nickname": "Amazing Grace" 
    }, 
    function(error){ 
    if(error){ 
     console.log("Data could not be updated." + error); 
    } else { 
     console.log("Data updated successfully."); 
    } 
    });