下午好,我在創建和寫入我的Firebase數據庫中的新端點時遇到困難。我正在嘗試創建一個名爲savedUser
的新子節點,並將該新創建的端點推送到我現有的user
節點的當前內容中。這裏是我使用的代碼:Firebase未創建新的指定端點
// this is the initial search that sets the data in the user
// endpoint
$("#searchButton").on("click", function(){
firebase.database().ref().once("child_added", function(snapshot){
// little lesson in closure
// dry coding
function ez(path){
return snapshot.child(path).val();
}
var dataObject = {
gamertag: ez("gamertag"),
totalKills: ez("totalKills"),
totalDeaths: ez("totalDeaths"),
totalGames: ez("totalGames")
};
//handlebars, getting template
var sourceTemplate = $("#list-template").html();
var template = Handlebars.compile(sourceTemplate);
//handlebars, sending object to DOM
var templateHTML = template(dataObject);
var $templateHTML = $(templateHTML);
$("#profileSearch").append($templateHTML);
});
});
var $confirmButton = $("#confirmButton");
// This is supposed to fire when the "save" button is clicked
$(document).on("click", "#confirm", function(event){
event.preventDefault()
// referencing database again to iterate and capture value
firebase.database().ref().once("value", function(snapshot){
function ez(path){
return snapshot.child(path).val();
}
// same procedure so far
var savedUserData = {
gamertag: ez("gamertag"),
totalKills: ez("totalKills"),
totalDeaths: ez("totalDeaths"),
totalGames: ez("totalGames")
}
function saveUser(newChildPath, data){
firebase.database().ref(newChildPath).set(data)
}
// call the function saves at the endpoint "savedUser"
saveUser("savedUser/", savedUserData);
});
});
上面的代碼沒有寫到我的數據庫,但基於導遊我一直在尋找我應該會成功這樣做。然而,這寫我的數據庫:
function saveUser(childPath, data){
firebase.database().ref(childPath).set(data)
}
saveUser("savedUser/", {new: "path"});
});
});
在我的數據庫控制檯,我可以找到「/ savedUser /新」,它更新沒有問題。但是,當我試圖從數據庫捕獲數據並將其格式化爲第一個示例時,它不起作用並且沒有任何內容被寫入。實際上,savedUser
端點已被刪除。我一定有一個微妙的缺失。我對Firebase不熟悉。任何幫助非常感謝,並告訴我是否可以提供更多信息。
謝謝!
也許Firebase不喜歡這種格式的'gamertag:ez(「gamertag」),'對象屬性?另外,試着只包括相關的代碼,也就是爲什麼要告訴我們關於把手的原因? –
'savedUserData'中的值是什麼?它們全都是空嗎?這是否真的是重現問題所需的最低代碼?似乎問題集可能會被簡化很多。另外,打開調試:'firebase.database.enableLogging(true);' – Kato
@Ursus @Kato例如,通過將'savedUserData'中的路徑更改爲像''user/gamertag'''這樣的工具,可以實現它的工作。然後所有東西都應該被髮送出去。感謝您的建議。 – m00saca