I am trying to get data of a certain userId out of a firebase Database. But nothing happens? I am creating an AngularFireObject of typ Profile
like this:
profileData: AngularFireObject<Profile>;
There is no error in the code. the HTML is shown how i prefer it but without the data.
the only thing what isn't correct, is that the Methode ionViewDidLoad is shown as unused. But when i pass the code inside the constructor it doesn't has any effect.
Can someone help me with that? Thanks a lot!
import { Component } from '#angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase, AngularFireObject} from 'angularfire2/database';
import {Profile} from '../../shared/models/profile';
/**
* Generated class for the HomePage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-home',
templateUrl: 'home.html',
})
export class HomePage {
profileData: AngularFireObject<Profile>;
constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase, private toast: ToastController,
public navCtrl: NavController, public navParams: NavParams) { }
ionViewDidLoad() {
this.afAuth.authState.subscribe(data => {
console.log(data.uid);
if (data && data.email && data.uid) {
console.log(data.email);
console.log(data.uid);
this.toast.create({
message: `Welcome to The APP, ${data.email}`,
duration: 5000
}).present();
this.profileData = this.afDatabase.object(`/profile/${data.uid}`)
console.log(this.profileData);
} else {
this.toast.create({
message: `Could not find authentication details`,
duration: 3000
}).present();
}
})
}
}
This is the HTML where i would like to load the data.
profile.ts
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {Profile} from '../../shared/models/profile';
import {AngularFireDatabase} from 'angularfire2/database';
#IonicPage()
#Component({
selector: 'page-profile',
templateUrl: 'profile.html',
})
export class ProfilePage {
profile = {} as Profile;
constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase,
public navCtrl: NavController, public navParams: NavParams) {
}
/*ionViewDidLoad() {
console.log('ionViewDidLoad ProfilePage');
}*/
createProfile(){
this.afAuth.authState.take(1).subscribe(auth => {
this.afDatabase.object(`profile/${auth.uid}`).set(this.profile)
.then(() => this.navCtrl.setRoot('HomePage'))
})
}
}
<p>Username: {{(profile | async)?.username }}</p>
<p>First Name: {{(profile | async) ?.lastName }}</p>
<p>Last Name: {{(profile | async) ?.firstName}}</p>
There is a issue in the path. your path has a userid which concatenated with profile without separating it using slash (/)
Try to re correct path,
this.afDatabase.object(`/profile/${data.uid}`)
To retrieve data and view, do this way
this.afAuth.authState.subscribe(data => {
if (data && data.email && data.uid) {
this.afDatabase.database.ref().child(`/profile/${data.uid}`).once('value', (snapshot) => {
let result = snapshot.val();
for (let k in result) {
this.profileData = result[k];
}
});
}
});
**This is the html **
<p>Username: {{ profileData.username }}</p>
<p>First Name: {{ profileData.lastName }}</p>
<p>Last Name: {{( profileData.firstName}}</p>
in Homepage.ts
you only have to add take(1) in ligne 27 just like that :
this.afAuth.authState.take(1).subscribe(data => {
Related
So I am relatively new to Ionic and Firebase, and I am trying to display user data that already exists in Firebase.
Here is the error that I am getting:
invalidpipeargument: '[object object']: error showing.
I do know what the issue is after looking at other questions similar to mine. It seems I cannot assign the async function as database.object does not return an observable. So I have tried to add .valueChanges(); to make it an observable, but I believe this is clashing with the angularfireobject as that is already assigned to the variable profile.data so this would not work.
I have also tried this:
// get (): AngularFireObject<any[]>{
//return this.afDatabase.list`/profile/${data.uid}`;
//}
If you point me in the right direction that would be great.
Here is my code:
.ts file
import { Component } from '#angular/core';
import { NavController, ToastController } from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, AngularFireObject} from 'angularfire2/database';
import { Profile } from '../../model/profile';
import { DEFAULT_INTERPOLATION_CONFIG } from '#angular/compiler';
#Component({
selector: 'page-about',
templateUrl: 'about.html'
})
export class AboutPage {
profileData: AngularFireObject<Profile>
// get (): AngularFireObject<any[]>{
//return this.afDatabase.list`/profile/${data.uid}`;
//}
constructor(private afAuth:AngularFireAuth, private afDatabase: AngularFireDatabase,
public navCtrl: NavController, private toast: ToastController) {
}
ionViewDidLoad() {
this.afAuth.authState.take(1).subscribe(data =>{
if (data && data.email && data.uid){
this.toast.create({
message: `Welcome to MatchMyFighter ${data.email}`,
duration: 3000,
}).present();
// this.profileData = this.afDatabase.object(`profile/${data.uid}`)
this.profileData = this.afDatabase.object(`/profile/${data.uid}`).valueChanges();;
}
else{
this.toast.create({
message:`could not autenticate`,
duration:3000
}).present;
}
})
}
}
.html
<p>
Username: {{(profileData | async)?.username}}
</p>
The call to valueChanges() converts the AngularFireObject<Profile> to an observable.
You have to change the type to Observable<Profile> like so:
profileData: Observable<Profile>;
constructor(private db: AngularFireDatabase) { } // other stuff emitted ...
ionViewDidLoad() {
// ...
this.profileData = this.db.object<Profile>(`/profile/${data.uid}`).valueChanges();
// ...
}
I always get this error:
TypeError: Cannot read property 'propName' of null
Here is my Typescript.
handlu-prop.ts
import { Component } from '#angular/core';
import { IonicPage, ModalController, NavController, NavParams, ActionSheetController } from 'ionic-angular';
import { AlertController } from 'ionic-angular';
import { AngularFireDatabase,FirebaseObjectObservable } from 'angularfire2/database';
import { Subscription } from 'rxjs/Subscription';
import { ConAgentsPage } from '../conagents/conagents';
import { HandluPage } from '../handlu/handlu';
import { PropertyItem } from '../../models/property-item/PropItem.interface';
#IonicPage()
#Component({
selector: 'page-handlu-prop',
templateUrl: 'handlu-prop.html',
})
export class HandluPropPage {
propertyItemSubscription: Subscription;
propertyItem = {} as PropertyItem;
propertyItemRef$: FirebaseObjectObservable<PropertyItem>;
constructor(private database: AngularFireDatabase,
private modCtrl: ModalController,
public navCtrl: NavController,
public navParams: NavParams,
public alertCtrl: AlertController) {
const propertyItemId = this.navParams.get('$key');
console.log(propertyItemId);
this.propertyItemRef$ = this.database.object(`Property/${propertyItemId}`).valueChanges();
this.propertyItemSubscription =
this.propertyItemRef$.subscribe( propertyItem => this.propertyItem = propertyItem );
}
ionViewWillLeave() {
this.propertyItemSubscription.unsubscribe();
}
handlu-prop.html (here is a part of my template)
<h2>{{propertyItem.propName}}</h2>
Try modifying your HTML file to this
<h2>{{ propertyItem?.propName }}</h2>
the ? serves the purpose of checking if this element is defined or not, it avoids crashing the app into that error.
I want to get the list of users where text=Groomers from firebase database
I am getting firebase warning about .indexOn, but not getting the list of users.
Below is my code:
import { Component, OnInit } from '#angular/core';
import { Router } from '#angular/router';
import { AuthService } from '../../services/auth.service';
import { AngularFireDatabase } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/Observable';
#Component({
selector: 'app-animal-services',
templateUrl: './animal-services.component.html',
styleUrls: ['./animal-services.component.css']
})
export class AnimalServicesComponent implements OnInit {
users: Observable<any[]>;
constructor(public _firebaseAuth: AngularFireAuth, private db: AngularFireDatabase, private router: Router) {
this.users = db.list('/professionals', ref => ref.orderByChild('text').equalTo("Groomers")).valueChanges();
}
ngOnInit() {
}
}
Below is the image of my database:
orderByChild("text") should be orderByChild("qualification")
Phew! I figured out my way by changing the database in the following way:
Below is the image of my database:
Currently I am able to post data to the firebase, but I can't get the data and display it in my ionic 3 app. This is my shoes.ts file:
import { Component } from '#angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { AngularFireDatabase } from "angularfire2/database";
import { FirebaseListObservable } from 'angularfire2/database';
#Component({
selector: 'page-shoes',
templateUrl: 'shoes.html',
})
export class ShoesPage {
shoppingItems: FirebaseListObservable<any[]>;
newItem = '';
getShoppingItems() {
return this.afDB.list('/shoppingItems/');
}
constructor(public navCtrl: NavController, public navParams: NavParams,
public afDB: AngularFireDatabase){
this.addItem("awesomeStuff");
this.getShoppingItems();
alert(this.shoppingItems);
}
ionViewDidLoad() {
console.log('ionViewDidLoad ShoesPage');
}
addItem(name) {
this.afDB.list('/shoppingItems/').push(name);
}
removeItem(id) {
this.afDB.list('/shoppingItems/').remove(id);
}
}
This is a part of my the shoes.html file:
<ion-list>
<ion-item class="text" *ngFor="let item of shoppingItems | async">
{{item.$value}}
</ion-item>
</ion-list>
Any ideas how I can be able to display the firebase data? Currently I am getting: [object Object] when I alert the shoppingList.
Iam trying to create a login page using firebase in ionic.I have imported al the neccessary componenets but i get the error Property 'login' does not exist on type 'AngularFireAuth' in the login method. Please help!
login.ts
import { Component } from '#angular/core';
import { NavController, NavParams, AlertController, LoadingController, Loading, IonicPage } from 'ionic-angular';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { AngularFireAuth } from 'angularfire2/auth';
#Component({
selector:'page-login',
templateUrl:'login.html'
})
export class LoginPage{
email: any;
password: any;
constructor(public NavCtrl: NavController,public NavParams,public angfire: AngularFireAuth){
}
ionViewDidLoad(){
console.log('ionViewDidload LoginPage');
}
login(){
this.angfire.login({
email:this.angfire,
password:this.password
},
})
}
}
For AngularFire2 try this
signInWithFacebook() {
this.afAuth.auth.signInWithPopup(new firebase.auth.FacebookAuthProvider())
.then(res => console.log(res));
}