1

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!

Ethan
  • 108
  • 2
  • 9

1 Answers1

0

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\n", 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.

putu
  • 6,218
  • 1
  • 21
  • 30