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!

2
  • How did you store the query result? Is it in a variable defined as map[string]interface{}? Commented Oct 9, 2017 at 23:55
  • Yes exactly like that Commented Oct 10, 2017 at 0:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.