下午好,我在创建和写入我的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