2016-06-28 38 views
0

下午好,我在創建和寫入我的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不熟悉。任何幫助非常感謝,並告訴我是否可以提供更多信息。

謝謝!

+0

也許Firebase不喜歡這種格式的'gamertag:ez(「gamertag」),'對象屬性?另外,試着只包括相關的代碼,也就是爲什麼要告訴我們關於把手的原因? –

+0

'savedUserData'中的值是什麼?它們全都是空嗎?這是否真的是重現問題所需的最低代碼?似乎問題集可能會被簡化很多。另外,打開調試:'firebase.database.enableLogging(true);' – Kato

+0

@Ursus @Kato例如,通過將'savedUserData'中的路徑更改爲像''user/gamertag'''這樣的工具,可以實現它的工作。然後所有東西都應該被髮送出去。感謝您的建議。 – m00saca

回答

0

我能得到這個代碼通過使像這樣的以下更改代碼的工作:

var savedUserData = { 
    gamertag: ez("user/gamertag"), 
    totalKills: ez("user/totalKills"), 
    totalDeaths: ez("user/totalDeaths"), 
    totalGames: ez("user/totalGames") 
} 

上面,我引用我想從拿數據的路徑。我需要這樣做,因爲我正在創建一個新的子項savedUser並將其添加到根目錄。這意味着當我只打電話ez("gamertag")時,代碼正在尋找名爲gamertag的根目錄中的某個東西,但確實存儲在user/gamertag中,必須添加一個savedUser節點。因此,我需要在代碼中更具體,在我的數據庫中,我正在搜索目錄中添加一個新文件並更完整地輸入路徑。