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 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作