maudulus
maudulus

Reputation: 11035

How to get values when returned firebase url for specific item

I am querying my firebase using the following, but can't seem to access the key value pairs under each firebase reference. How can I get those values:

//PLEASE NOTE that snap.val().key is a key for another database (in this example -KjexampleZoe5-1exampleJp2UY) 
var desiredItem = this.goodsData.findGoodById(snap.val().key);
// => https://example-url.firebaseio.com/exampleList/-KjexampleZoe5-1exampleJp2UY

//find good by id
findGoodById(goodId): firebase.database.Reference {
    return this.exampleList.child(goodId);
}

But when I try to get values using desiredItem.val().key2 or desiredItem.key2 I get Property 'key2' does not exist on type 'Reference' or Property 'val' does not exist on type 'Reference':

-KjexampleZoe5-1exampleJp2UY
  |
  |
  - key: "value"
  |
  |
  - key2: "value2"
  |
  | 
  - key3: "value3"

Upvotes: 0

Views: 641

Answers (2)

MehulJoshi
MehulJoshi

Reputation: 889

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

Then you can write something like

ref.orderByChild("userkey").equalTo("11112").once("value", function(snapshot) {
    console.log(snapshot.key);
});

Upvotes: 1

Aldo Sanchez
Aldo Sanchez

Reputation: 450

Try this:

return firebase.database().ref('/myRef/').once('value').then(function(snapshot) {
      var obj = snapshot.val(); // should be complete object;
      var keyValue = snapshot.val().key //should be object property value
    });

Upvotes: 1

Related Questions