如何使用golang从Firebase中删除一个键?

huangapple go评论85阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2017年1月24日 14:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/41821290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定