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 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号