从Ionic 2获取Firebase数据 [英] Get data from Firebase in Ionic 2

查看:130
本文介绍了从Ionic 2获取Firebase数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用离子2写了我的应用程序,然后按照 Josh Morony教程,但我不知道如何从我的firebase数据库中获取特定元素。



例如I有这样的树:

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ | _ name:'toto'

等等...



我试过这种方式:

  elt:FirebaseListObservable< any []> 
this.elt = af.database.list('/ user');

但我该如何处理所选的数据?

firebaseListObservable 事件触发返回数据。

在您的提供者 .ts

  getData(fbPath):Promise< any> {
return new Promise(resolve => {
this.db.list(fbPath).subscribe(data => {
resolve(data);
})






这里的承诺解决了一旦 data 被填充并返回一个数组,可以很容易地访问 $ value $ key 属性。这是创建条件或复杂查询或具有通用属性的提供者服务的理想选择(而不是直接查询 firebaseListObservable 的快照)

在你的控制器中,你可以写一些像

  this.providerName.getData('users')。然后(data => {
console.log('data',data);
})

这将返回一个对象字面值,它的值是
$ b $ ul
$ li <$ li

  • $ key

  • $ value



  • 现在如果你想要一个匹配的条件,通过表 users $ key 的匹配条件,通过 data 如果(myUserIdVar === data。$ key){//在这里做些什么}; code

    $ pre $

    使用类似于 lodash 例如,如果你想要一个条件匹配一个存储的ID,比如说 firebase.auth()。currentUser.uid 您可以做一个简单的 _。find

     从'lodash'导入{find}; 
    从'firebase'导入*作为firebase; //或者只是firebase auth的东西
    ...

    let filteredUser = find(data,['$ key',firebase.auth()。currentUser.uid])

    $ key 值将等于 | __(user_id)

    I have written my app with ionic 2 and followed the tutorial of Josh Morony, but I don't know how I can get a specific element from my firebase database.

    For example I have this tree :

    user
    
    |__ (user_id)
    
      |_ name : 'toto'
    

    And so on...

    I tried this way:

    elt: FirebaseListObservable<any[]>;
    this.elt = af.database.list('/user');
    

    But how can I work with the selected data?

    解决方案

    In order to get the data as an array from a provider you need to return a promise which will be returned once the firebaseListObservable event is triggered with return data.

    In your provider .ts

    getData(fbPath): Promise<any> {
      return new Promise(resolve => {
        this.db.list(fbPath).subscribe(data => {
          resolve(data);
        })
      })
    }
    

    Here the promise resolves once the data is populated and returns an array with easy access to the $value and $key properties. Which is ideal for creating conditionals or complex queries or a provider service with generic properties ( as opposed to querying the snapshot of the firebaseListObservable directly )

    In your controller you can then write something like

     this.providerName.getData('users').then(data => {
        console.log('data',data);
      })
    

    This will return an object literal with the values

    • $exists
    • $key
    • $value

    So now if you want a match conditional you can loop through the data with the match condition on the $key of the table users

    if(myUserIdVar === data.$key){ // do something here };
    

    A tidier syntax can be found using a library like lodash Where for example if you want a condition to match a stored id, say firebase.auth().currentUser.uid you can do a simple _.find

     import { find } from 'lodash';
     import * as firebase from 'firebase'; // Or just the firebase auth stuff
     ...
    
     let filteredUser = find(data, ['$key', firebase.auth().currentUser.uid])
    

    The $key value will be equal to the |__ (user_id) value

    这篇关于从Ionic 2获取Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆