A search item object with a specific key value in the firebase database

I am trying to do a search with a specific value with userkey in a firebase database, but I am getting below problems.

I want to receive, as if I transferred the user key 11112, then when recording i11113 two records will start, then one record will come.

enter image description here

Despite the fact that I tried with the code below, but getting an error. firebase script: -

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

the 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
+4
source share
3 answers

There are two problems in the code.

  • As mentioned in @theblindprophet, you should use orderByChildinstead orderBy.
  • SDK firebase. , firebase.

    , SDH 3.1

    <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);
    

    , , Add Firebase to your Web app.

    , ref, .

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

jsFiddle, .

+4

:

Uncaught TypeError: ref.orderBy

, orderBy, , .

orderByChild.

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

: orderByChild equalTo

+2

Take a snapshot of uid values. Then compare the user key with the search parameter that you go through.

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) {
    ...
  }
});
0
source

Source: https://habr.com/ru/post/1649161/


All Articles