javascript - Search elements object with specific key value in firebase database - Stack Overflow

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

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 e when i pass 11113 then one record will e.

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

 <script type='text/javascript' src='.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

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 e when i pass 11113 then one record will e.

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

 <script type='text/javascript' src='https://cdn.firebase./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
Share Improve this question edited Jul 26, 2016 at 13:24 adolfosrs 9,3896 gold badges44 silver badges69 bronze badges asked Jul 26, 2016 at 12:46 Bhaskar BhattBhaskar Bhatt 1,48313 silver badges19 bronze badges 1
  • What error are you getting? – gcampbell Commented Jul 26, 2016 at 12:48
Add a ment  | 

3 Answers 3

Reset to default 4

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./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.

Your error:

Uncaught TypeError: ref.orderBy is not a function

is telling it can't find the function orderBy and that is because it doesn't exist.

You are looking for orderByChild.

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

Reference: orderByChild and equalTo

Grab a snapshot of the uid values. Then pare userkey with the search parameter you pass in.

var rootRef = new Firebase('fire-base-url');
var userRef = rootRef.child(user.uid);
userRef.on('value', function(snapshot){
  var myDbKey = snapshot.child("userkey");
  if (mySearchKey === myDbKey) {
    ...
  }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744125047a4559576.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信