如何选择所有用户数据,Ionic Firebase

时间:2017-07-29 15:42:12

标签: typescript firebase ionic-framework firebase-realtime-database

我有usersProfile表,其中uid保存数据

enter image description here

如何选择所有用户的uid和数据? 我只能选择授权用户数据。

getUsersData(token: string) {
    const userId = this.authService.getActiveUser().uid;
    return this.http.get('https://mainapp.firebaseio.com/usersProfile/' + userId + '.json?auth=' + token)
        .map((response: Response) => {
            const usersData: UsersData[] = response.json() ? response.json(): [];                
            return usersData;
        })    
        .do((userData : UsersData[]) => {
            if (userData) {
                this.usersData = userData;
            }
            else {
                this.usersData = [];                
            }
        })
}

1 个答案:

答案 0 :(得分:1)

如果您想使用firebase,则必须阅读此article first。首先,您需要为您的Ionic项目添加firebase 安装& JavaScript中的安装程序初始化实时数据库JavaScript SDK。

只需npm install firebase

在你的app.module.ts中创建firebase配置对象:

// Set the configuration for your app
// TODO: Replace with your project's config object
var config = {
  apiKey: "apiKey",
  authDomain: "projectId.firebaseapp.com",
  databaseURL: "https://databaseName.firebaseio.com",
  storageBucket: "bucket.appspot.com"
};
firebase.initializeApp(config);

// Get a reference to the database service
var database = firebase.database();

您可以在Firebase console中找到所有设置。有关更多信息,请阅读Firebase文档。

在你的角度服务中,你可以导入firebase并向数据库发出请求:

import * as firebase from 'firebase';

// Service declaration here
// code here ...

getUsersData() {
    const userId = this.firebase.auth().currentUser.uid;
    return firebase.database().ref('usersProfile')
                            .once('value')        
                            .then(snapshot => snapshot.val())    
                            .then(users => console.log(users));
}

您可以找到更多信息here