How to select all users data, Ionic Firebase

假装没事ソ 提交于 2021-02-08 06:46:33

问题


I have usersProfile table, where save data by uid

enter image description here

How to select all users uid and data? I can select only Authorized users data.

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:


If you want use firebase you must read this article first. First of all you need add firebase to your Ionic project and Installation & Setup in JavaScript initialize the realtime database JavaScript SDK.

Just do npm install firebase

In your app.module.ts create firebase config object:

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

You could find all setting in your Firebase console. For more information read Firebase documentation.

After that inside you angular service you could import firebase and do request to database:

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

More information you could find here



来源:https://stackoverflow.com/questions/45391075/how-to-select-all-users-data-ionic-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!