【问题标题】:Retrieving child value of unknown parent Firebase, AngularJS检索未知父Firebase,AngularJS的子值
【发布时间】:2017-11-09 03:32:58
【问题描述】:

我有一个关于 Firebase 的基本项目,AngularJS。

    {
  "users" : {
    "X3IuXTMi9RgOLMKBoqPJtB1JGGQ2" : {
      "profile" : {
        "firstName" : "test",
        "lastName" : "5",
        "username" : "test5"
      }
    },
    "poPg2BfHA1gq3qtXNLbuw4X2Zt23" : {
      "profile" : {
        "firstName" : "test3",
        "lastName" : "asdfg",
        "username" : "test3"
      }
    }
  }
}

我想基本上按用户名添加用户。当在 alertprompt(ionic) 上输入用户名并单击添加时,它将找到目标用户名并在其父级上获取 uid。它会在“users/$targetuid/friendrequests/$senderuid”上写入数据

我最接近的搜索结果是:Retrieve data of child of unknown parent ID - Firebase

但我不能使用任何 .ref 代码;

Cannot read property 'ref' of undefined

我只能使用 FirebaseObjectObservable,我做错了什么?为什么我不能用

firebase.database().ref

或任何基于 .ref 命令的类似代码?

这里基本上什么都做不了:https://firebase.google.com/docs/database/web/read-and-write

我对编码有点陌生,所以如果它简单或有趣,不要笑太多。感谢阅读。

【问题讨论】:

    标签: angularjs firebase ionic-framework ionic3 angularfire


    【解决方案1】:

    我可以在这里看到两种解决方案:

    1) 创建一个 usernameToUserid 映射:在 firebase 上创建一个新资源 usernameToUserid,如下所示:

    {
      "users" : {
        "X3IuXTMi9RgOLMKBoqPJtB1JGGQ2" : {
          "profile" : {
            "firstName" : "test",
            "lastName" : "5",
            "username" : "test5"
          }
        },
        "poPg2BfHA1gq3qtXNLbuw4X2Zt23" : {
          "profile" : {
            "firstName" : "test3",
            "lastName" : "asdfg",
            "username" : "test3"
          }
        }
      },
      "usernameToUserid: {
        "test5": "X3IuXTMi9RgOLMKBoqPJtB1JGGQ2",
        "test3": "poPg2BfHA1gq3qtXNLbuw4X2Zt23"
      }
    }
    

    这样您可以检索 userId,然后检索带有用户名的用户配置文件。

    2) 检索所有用户并在本地搜索用户名,糟糕的主意。

    【讨论】:

    • 好主意!但是我如何从“usernameToUserid/”中检索 uid 数据我不能使用 firebase.database() 函数的任何功能,为什么它不像在网络上那样
    • 获取uid,在收到“添加好友”提示输入的用户名后,查询usernameToUserid/username,返回uid。我不明白你在使用 firebase 时遇到了什么问题,在 Ionic 中它应该与 web.xml 完全相同。您是否使用此firebase.google.com/docs/database/web/start 正确配置了它?
    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多