搜索元素对象在Firebase数据库中具有特定的键值 [英] Search elements object with specific key value in firebase database

查看:146
本文介绍了搜索元素对象在Firebase数据库中具有特定的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用firebase数据库中的userkey搜索具体的值,但我得到的问题。



我想获取喜欢如果我通过用户密钥11112那么当我传递11113时会有两条记录出现,然后就会有一条记录来。



即使我已经尝试了下面的代码,但得到错误。
firebase脚本:

 < script type ='text / javascript'src ='https:// cdn .firebase.com / JS /客户端/ 1.0.15 / firebase.js'>< /脚本> 

代码:

  var ref = new Firebase('fire-base-url'); 
ref.orderBy(userkey)。equalTo(11112)。once(value,function(snapshot){
console.log(snapshot.key);
}) ;

控制台错误:

  Uncaught TypeError:ref.orderBy不是函数


解决方案

你的代码有两个问题。


  1. 正如@theblindprophet已经提到的,您应该使用 orderByChild 来代替 orderBy

  2. 您使用的是旧的Firebase SDK。它不适用于新的Firebase控制台中创建的应用程序。



    请确保使用新的3.1 firebase sdk与

     < script type ='text / javascript'src ='https://www.gstatic.com/firebasejs/3.1.0/firebase.js'>< / script> 

    然后您应该使用

      var config = {
    apiKey:,
    authDomain:,
    databaseURL:,
    storageBucket:,
    };
    firebase.initializeApp(config);

    您可以通过转到控制台,点击应用程序名称以及按下将Firebase添加到您的Web应用程序

    然后获取 ref c $ c>对象,你将需要下面的代码。



    var ref = firebase.database()。ref();



  3. 看看这个 jsFiddle 查看完整的工作示例。


    I am trying to search with specific value with of userkey in firebase database but i am getting below issues.

    I want to fetch like if i pass user key 11112 then two records will come when i pass 11113 then one record will come.

    Even though I have tried with below code but getting error. firebase script :-

     <script type='text/javascript' src='https://cdn.firebase.com/js/client/1.0.15/firebase.js'></script>
    

    Code:

    var ref = new Firebase('fire-base-url');
    ref.orderBy("userkey").equalTo("11112").once("value", function(snapshot) {
      console.log(snapshot.key);
    });
    

    Console Error:

    Uncaught TypeError: ref.orderBy is not a function
    

    解决方案

    There is two problems with your code.

    1. As @theblindprophet already mentioned, you should be using orderByChild instead of orderBy.
    2. You are using the old firebase SDK. It won't work with applications created in the new firebase console.

      Please make sure to use the new 3.1 firebase sdk with

      <script type='text/javascript' src='https://www.gstatic.com/firebasejs/3.1.0/firebase.js'></script>
      

      Then you should initialize your app using

        var config = {
          apiKey: "",
          authDomain: "",
          databaseURL: "",
          storageBucket: "",
        };
        firebase.initializeApp(config);
      

      You will be able to get your config details by going to the console, clicking in your application name and pressing Add Firebase to your Web app.

      Then to get your ref object you will need the code bellow.

      var ref = firebase.database().ref();

    Take a look in this jsFiddle to see a full working example.

    这篇关于搜索元素对象在Firebase数据库中具有特定的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆