how to read a specific record from firebase database using golang?

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

how to read a specific record from firebase database using golang?

问题

数据库结构:

user json:

{  
    "-KbF1E26gjZCjPnfcOmi" : {
        "DateOfCreation" : 1485253960222,
        "EmailId" : "abc@gmail.com",
        "FirstName" : "abcd",
        "LastName" : "z z",
        "Status" : "active",
        "UserType" : "admin"
    }
}

如何检索与此**key(-KbF1E26gjZCjPnfcOmi)**对应的所有数据?

我尝试了这段代码,但它不起作用。

func (m *User) RetrieveFromDBId(ctx context.Context,key string)(bool) {
    dB, err := GetFirebaseClient(ctx,"")
    var s []string
    err = dB.Child("User").Child(key).Value(s)
    if err != nil {
        log.Fatal(err)
        return false
    }
    log.Println( s)
    return true
}
英文:

Database structure:

user json:

{  
    "-KbF1E26gjZCjPnfcOmi" : {
        "DateOfCreation" : 1485253960222,
        "EmailId" : "abc@gmail.com",
        "FirstName" : "abcd",
        "LastName" : "z z",
        "Status" : "active",
        "UserType" : "admin"
    }
}

How to retrieve whole data corresponding to this
key(-KbF1E26gjZCjPnfcOmi)

I have tried this code, but it is not working.

func (m *User) RetrieveFromDBId(ctx context.Context,key string)(bool) {
    dB, err := GetFirebaseClient(ctx,"")
    var s []string
    err = dB.Child("User").Child(key).Value(s)
    if err != nil {
        log.Fatal(err)
        return false
    }
    log.Println( s)
    return true
}

答案1

得分: 2

我得到了答案

func (m *User) RetrieveFromDBId(ctx context.Context, key string) (bool) {

    dB, err := GetFirebaseClient(ctx, "")
    value := User{}
    err = db.Child("/User/"+key).Value(&value)
    if err != nil {
        log.Fatal(err)
        return false
    }
    log.Println(value)
    return true
}

这是一个用于从数据库中检索用户信息的函数。它使用了一个名为GetFirebaseClient的函数来获取Firebase客户端,并通过指定的key从数据库中获取用户信息。如果出现错误,它会记录错误并返回false,否则它会打印用户信息并返回true

英文:

I got answer

func (m *User) RetrieveFromDBId(ctx context.Context,key string)(bool) {

       dB, err := GetFirebaseClient(ctx,"")
       value := User{}
       err = db.Child("/User/"+key).Value(&value)
           if err != nil {
              log.Fatal(err)
              return false
            }
     log.Println( value)
     return true
}

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

发表评论

匿名网友

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

确定