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.

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

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助