首页 文章

如何使用Firebase在Ionic 3中通过参数检索数据

提问于
浏览
1

我在Firebase上有一个实时数据库,我想使用参数从这个数据库中检索记录 .

Here is the screeshot from that

例如,在您看到的层次结构中,我想使用uID值获取数据 . 我怎样才能做到这一点?

UPDATE

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

我在关于我给出的链接的文章中尝试了“阅读Firebase数据” Headers ,但是如果它是为Ionic 2编写的,我不知道如何更新代码 .

这是我写的代码,

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

}

根据我必须做的文章创建一个类型为“Reference”的变量,但是使用AngularFireDatabase时,不可能进行这样的定义 .

如何在Ionic 3中更新此定义?

UPDATE

我添加了一些代码;

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

和输出;

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

我可以得到数据,但答案不明确 .

Firebase使用键值记录所有数据 . 我还在“/ Post /”值之后添加了此键值作为数据路径 . 例如“/ Post / -1ersksnu0nsw1 /” .

这是代码;

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

我可以获得真正的 Value ,但我仍然不知道如何根据他们的记录列来做到这一点 .

2 回答

  • 0

    你可以查看这个tutorial . 它创建了一个聊天应用程序,但它可以帮助您实现您想要的目标 .

    firebase.database().ref('/Post/').on('value', resp => {
        console.log(resp)
    });
    
  • 0

    找到了!

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

    这段代码就是这样做的 .

相关问题