dqwh26750 2019-03-28 20:41
浏览 99
已采纳

如何从Firebase获取多个记录?

I have a firebase db with data like below: enter image description here

I want to pull multiple records based on certain criteria. I've figured out how to pull a single record based on an ID using the method below:

ref := fbDB.NewRef("/Event/123")
event := data.EventData{}
if err := ref.Get(c, &event); err != nil {
   // error handling stuff
}

This loads event with the data I would expect. When I try to modify this code to select multiple records with the code below:

type EventResults struct {
    Events []data.EventData
}
...
ref := fbDB.NewRef("/Event")
res := EventResults{}
if err := ref.Child("candy").OrderByValue().StartAt(350).Get(c, &res); err != nil {
   //error handling stuff
}

res.Events is always an empty array (and err is nil).

Can anyone see what I'm doing wrong?

  • 写回答

2条回答 默认 最新

  • dongma2388 2019-03-28 21:29
    关注

    It's empty because when retrieving data you need to traverse the database and pass through each node.

    In your case you have this:

    ref.Child("candy");
    

    Here ref is referencing the node Event and under Event you have different Ids (123, 789), therefore you need to access these nodes before trying to access node candy.


    If you want to retrieve a list of candy that are under Event, then you should iterate inside the direct child of node Event then you will be able to access node candy.

    Example:

    f := firego.New("https://my-firebase-app.firebaseIO.com", nil)
    var res map[string]interface{}
    
    ref := fbDB.Ref("/Event")
    if err := ref.OrderBy("candy").StartAt(3).Value(&res); err != nil {
    
    }
    

    You need to use the OrderBy query to be able to retrieve result based on the value of of candy. For example, in my code OrderBy("candy").StartAt(3), this query will give you a result where the value of candy starts with 3. candy :300

    Check the docs:

    https://godoc.org/gopkg.in/zabawaba99/firego.v1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失