[英]Add a new child in Firebase
因此,我的情况与在线发现的问题有所不同。
我希望在按下按钮时使用新的子节点Foot19
更新节点Societies
。 上下文是学校群组的列表,当用户按下按钮以“跟随”学校群组时,该群组的ID将添加到其个人数据部分。
因此,当他们单击addFootballSoc
按钮时, Foot19
将作为新的子节点添加到其特定的Societies
节点。
每个sUsers
节点开始处的sUsers
等同于其authentication uid
,这就是为什么我使用firebase.auth()
调用它的原因
document.getElementById('addFootballSoc').onclick = function() {addFootball()};
function addFootball(){
var runUid = firebase.auth().currentUser.uid;
var searchRef = firebase.database().ref('sUsers');
var newSearchRef = searchRef.child(runUid);
var mm = newSearchRef.child('Societies')
mm.child.set('Foot19'){}
}
我希望它为加入的每个组添加一个新的子级,这样他们就不能覆盖它们,例如,如果我要添加“艺术”组按钮
编辑:
更改了功能,以便onclick可以正常工作,并且现在可以将其写入我想要的位置,但是,如果我要在Foot19
旁边添加另一个子节点,它将删除Foot19
并将其替换,我会使用push
但我不想使用添加到我的节点的唯一密钥。 如何更改此设置以确保可以添加更多节点?
document.getElementById('addFootballSoc').onclick = function() {addFootball()};
function addFootball(){
var runUid = firebase.auth().currentUser.uid;
var searchRef = firebase.database().ref('sUsers');
var newSearchRef = searchRef.child(runUid);
var mm = newSearchRef.child('Societies')
mm.child.set('Foot19'){}
}
您可能会在页面加载时立即调用函数addFootball()
,然后才能从Firebase退回/解析uid或看起来像在处理.set
错误的数据类型。 尝试这个。
document.getElementById('addFootballSoc').addEventListener("click", function(evt) {
var runUid = firebase.auth().currentUser.uid;
if(runUid){
var searchRef = firebase.database().ref('sUsers');
var newSearchRef = searchRef.child(runUid);
var mm = newSearchRef.child('Societies')
mm.child.set({'Foot19':true});
} else {
console.log("no uid");
}
});
解决了。
屏幕抓图显示了条目放置在用户ùid
节点内的子节点societies
中。 我更改了第6行的值以测试新的节点不会被覆盖,因此在整个ALT5
和ASN11
进行了额外的更改。
function addFootball(){
var runUid = firebase.auth().currentUser.uid;
var searchRef = firebase.database().ref('sUsers');
var newSearchRef = searchRef.child(runUid);
newSearchRef.child('Societies').update({
Foot19 : true
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.