1

I have a real-time database on Firebase, and I want to retrieve records from this database using parameters.

Here is the screeshot from that

For example, in the hierarchy you see, I want to get the data using the uID value. How can I do that?

UPDATE

https://javebratt.com/firebase-objects-ionic-2-app/

I have tried the heading "Read Firebase Data" in the article on the link I gave, but I do not know how to update the code if it is written for Ionic 2.

Here is the code I wrote,

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Post } from '../../models/models/post';
import { AngularFireDatabase } from 'angularfire2/database';
import { Storage } from '@ionic/storage';

@IonicPage()
@Component({
  selector: 'page-profile',
  templateUrl: 'profile.html',
})

export class ProfilePage {

  public posts: Post[];
  public uID: string;

  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    private db: AngularFireDatabase,
    private storage: Storage) {

    this.storage.get('uID').then((val) => {
      console.log('uID: ' + val);
      this.uID = val;
      console.log('this.uID: ' + this.uID);

      this.db.list<Post>('/Post/',
        ref => ref.orderByChild('uID').equalTo(this.uID)).valueChanges().subscribe(t => {
          this.posts = t;
        })
    });

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ProfilePage');
  }

}

According to the article I had to do create a variable which is type of "Reference", but with AngularFireDatabase it is not possible to make such a definition.

How do I update this definition in Ionic 3?

UPDATE

I added some codes;

db.database.ref('/User/').on('value', res => {
      console.log("Try Val: " + res.val)
      console.log("Try Key: " + res.key)
      console.log("Try Child: " + res.child)
    });

And outputs;

Try Val: function () {
        util_1.validateArgCount('DataSnapshot.val', 0, 0, arguments.length);
        return this.node_.val();
    }
Try Key: User
Try Child: function (childPathString) {
        util_1.validateArgCount('DataSnapshot.child', 0, 1, arguments.length);
        // Ensure the childPath is a string (can be a number)
        childPathString = String(childPathString);
        validation_1.validatePathString('DataSnapshot.child', 1, childPathString, false);
        var childPath = new Path_1.Path(childPathString);
        var childRef = this.ref_.child(childPath);
        return new DataSnapshot(this.node_.getChild(childPath), childRef, PriorityIndex_1.PRIORITY_INDEX);
    }

UPDATE

I could get data, but it's not clear answer.

Firebase records all data with a key value. I also added this key value after the "/ Post /" value as the data path. For example "/ Post / -1ersksnu0nsw1 /".

Here is the code;

 db.database.ref('/User/-L88gtymS5pS3KWtZrmI').on('value', res => {
      console.log(res.val().email)
    });

And I could get true value, but I still do not know how to do this according to the columns of their records.

5

2 Answers 2

1

EUREKA!

db.database.ref('/User/').orderByChild('uID').equalTo(this.uID).once('value', (snapshot) => {
   console.log(snapshot.val().email)
})

This code will do that.

0

You can checkout this tutorial. It create a chat app but it may help you on what you want to to.

firebase.database().ref('/Post/').on('value', resp => {
    console.log(resp)
});
1
  • Hi thanks for your answers, but I could not use "firebase.database().ref...", I injected a AngularFirebaseDatabase variable in my constructor and it has none of those used like yours.
    – Umut Gür
    Commented Mar 24, 2018 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.