英文:
How to delete a key from Firebase using golang?
问题
这是我的Firebase结构。
联系人
{
"-Kav_cy_EFZnu5vNWP2g" :
{
"Address" : "dfsswrfdssddsd",
"CurrentDate" : 1484910921411,
"Email" : "a@fg.hh",
"Name" : "abc",
"PhoneNumber" : "53465465765677",
"State" : "fdsf",
"Status" : "Completed",
"Zipcode" : "43434"
},
"-Kb9ghONh670tUOkOG3N" :
{
"Address" : "jfhjhfjfj",
"CurrentDate" : 1485164432603,
"Email" : "fg@gmail.com",
"Name" : "dcf",
"PhoneNumber" : "9897878787",
"State" : "fdsf",
"Status" : "Completed",
"Zipcode" : "56546"
}
}
我想删除与键(-Kav_cy_EFZnu5vNWP2g
)对应的数据。我尝试了下面的代码,但它不起作用:
模型
func (m *User) DeleteFromDB(ctx context.Context,key string)(bool) {
dB, err := GetFirebaseClient(ctx,"")
if err!=nil{
log.Println("连接错误:",err)
}
err = dB.Child(key).remove();
if err!=nil{
log.Println("删除错误:",err)
return false
}
return true
}
在DeleteFromDB()
函数中,我有这个键。
英文:
this is my Firebase structure.
Contacts
{
"-Kav_cy_EFZnu5vNWP2g" :
{
"Address" : "dfsswrfdssddsd",
"CurrentDate" : 1484910921411,
"Email" : "a@fg.hh",
"Name" : "abc",
"PhoneNumber" : "53465465765677",
"State" : "fdsf",
"Status" : "Completed",
"Zipcode" : "43434"
},
"-Kb9ghONh670tUOkOG3N" :
{
"Address" : "jfhjhfjfj",
"CurrentDate" : 1485164432603,
"Email" : "fg@gmail.com",
"Name" : "dcf",
"PhoneNumber" : "9897878787",
"State" : "fdsf",
"Status" : "Completed",
"Zipcode" : "56546"
}
}
i want to delete data corresponding to the key(-Kav_cy_EFZnu5vNWP2g
). I have tried below code, but it's not working:
model
func (m *User) DeleteFromDB(ctx context.Context,key string)(bool) {
dB, err := GetFirebaseClient(ctx,"")
if err!=nil{
log.Println("Connection error:",err)
}
err = dB.Child(key).remove();
if err!=nil{
log.Println("Deletion error:",err)
return false
}
return true
}
in DeleteFromDB()
, I have the key.
答案1
得分: 2
尝试将remove函数更改为以下形式:
dB.Child("/Contacts/" + key).Remove()
英文:
try changing the remove function like this
dB.Child("/Contacts/"+key).Remove()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论