doujiena0025 2017-10-09 23:17
浏览 75
已采纳

如何从Firebase数据库打印特定字段

I am a complete firebase newbie, but basically my situation is this I am collecting weather data and storing it in firebase like so:

{
    "-Kw2H2dbJKZbbg-6LA6b": {
        "date": "10/9/2017",
        "data": "filler test data",
    }
}

And then I will display this info to my website. I am using Go with firego as my backend and I am wondering how could I query my db and print any individual field. For example I wish to display the date

"date": "10/9/2017"

Just in general how can this be done. Any help is much appreciated!

  • 写回答

1条回答 默认 最新

  • douchengchu8374 2017-10-10 04:00
    关注

    If the query result is stored as map[string]interface{} then iterate over the results as:

    //queryResults := ...
    for _, val := range queryResults {
        //test whether vmap is an object/not
        vmap, ok := val.(map[string]interface{})
        if !ok {
            continue
        }
    
        //vmap contains {"date":..., "data":...}
        field := "date"
        if v, ok := vmap[field]; ok {
            fmt.Printf("%s: %v
    ", field, v)
        }       
    }
    

    If you don't know the object's hierarchy, you can implement a kind of find function to search for a specific key/field from queryResults. See https://stackoverflow.com/a/45757396/6207052.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?