Setting Firebase data to property in html file












1















Okay so i am fairly new at ionic and i am experiencing this problem where by i am getting the users data from firebase but whenever i set it to the public variable and try and reference it in the html file, i am getting a log error of "cannot set 'variable name' to property of undefined". Here is my code for a more clearer explanation and understanding of what i am trying to achieve. Thank you.



.ts file:



import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,AlertController,
LoadingController, Loading } from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import { MenuPage } from '../menu/menu';
import firebase from 'firebase';
import { first } from 'rxjs/operators';



@IonicPage()
@Component({
selector: 'page-account',
templateUrl: 'account.html',
})
export class AccountPage {

public userinfo;

constructor(public navCtrl: NavController, public navParams: NavParams,
private alertCtrl:AlertController, public fAuth:AngularFireAuth,
public loading:LoadingController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad AccountPage');
this.readData();
}

isLoggedIn(){
return this.fAuth.authState.pipe(first()).toPromise();
}

userStatus(){
const user = this.isLoggedIn()
if(user){
console.log('logged in');
this.readData();
}else{
}
}


async readData(){
let load = this.loading.create({
content: "Setting up your profile...",
spinner:'dots'
});
load.present();
await this.fAuth.authState.subscribe((user:firebase.User) =>{
if(user){
firebase.database().ref('/users/' +
user.uid).once('value').then(function(snapshot){
if(snapshot.exists()){
var data = (snapshot.val() && snapshot.val().username) ||
'Anonymous';
//this.userinfo = data;
console.log(data);
}else{
console.log("i need a name");
}});
}else{
console.log("not logged in, log in please");
this.alertLogin();
};
});
console.log(this.userinfo );
load.dismiss();
}


getName(){
let alert = this.alertCtrl.create({
title: 'Hello new friend! :) please can you tell us your name...',
inputs: [
{
name: 'name',
placeholder: 'name'
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: data => {
console.log('Cancel clicked');
this.navCtrl.setRoot(MenuPage);
}
},
{
text: 'Confirm',
handler: data => {
this.fAuth.authState.subscribe((user:firebase.User) =>{
firebase.database().ref('/users/' + user.uid).set({
username:data.name
});
});
}
}
]
});
alert.present();
}

alertLogin(){
//if user is not already logged in
let alert = this.alertCtrl.create({
title: 'Whoa there Sally! you need to log in first! :)',
inputs: [
{
name: 'email',
placeholder: 'email'
},
{
name: 'password',
placeholder: 'Password',
type: 'password'
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: data => {
console.log('Cancel clicked');
this.navCtrl.setRoot(MenuPage);
}
},
{
text: 'Login',
handler: data => {
this.login(data.email,data.password);
}
},{
text: 'Register',
handler: data => {
this.register(data.email,data.password);
}
}
]
});
alert.present();
}

async login(email,password){
try{
var login = await this.fAuth.auth.signInWithEmailAndPassword(
email,
password
);
if(login){
console.log("Successfully logged in!");
}
}catch(err){
console.error(err);
alert("Sorry we couldnt find you in our system :(");
this.navCtrl.setRoot(MenuPage);
}
}

async register(email,password){
try{
var reg = await this.fAuth.auth.createUserWithEmailAndPassword(
email,
password
);
if(reg){
this.getName();
console.log("successfully registered!");
this.navCtrl.setRoot(AccountPage);
}
}catch(err){
console.error(err);
}
}

logout(){
this.fAuth.auth.signOut();
}

}


.html file:



<ion-item>
<h1>{{userinfo}}</h1>
</ion-item>









share|improve this question



























    1















    Okay so i am fairly new at ionic and i am experiencing this problem where by i am getting the users data from firebase but whenever i set it to the public variable and try and reference it in the html file, i am getting a log error of "cannot set 'variable name' to property of undefined". Here is my code for a more clearer explanation and understanding of what i am trying to achieve. Thank you.



    .ts file:



    import { Component } from '@angular/core';
    import { IonicPage, NavController, NavParams,AlertController,
    LoadingController, Loading } from 'ionic-angular';
    import {AngularFireAuth} from 'angularfire2/auth';
    import { MenuPage } from '../menu/menu';
    import firebase from 'firebase';
    import { first } from 'rxjs/operators';



    @IonicPage()
    @Component({
    selector: 'page-account',
    templateUrl: 'account.html',
    })
    export class AccountPage {

    public userinfo;

    constructor(public navCtrl: NavController, public navParams: NavParams,
    private alertCtrl:AlertController, public fAuth:AngularFireAuth,
    public loading:LoadingController) {
    }
    ionViewDidLoad() {
    console.log('ionViewDidLoad AccountPage');
    this.readData();
    }

    isLoggedIn(){
    return this.fAuth.authState.pipe(first()).toPromise();
    }

    userStatus(){
    const user = this.isLoggedIn()
    if(user){
    console.log('logged in');
    this.readData();
    }else{
    }
    }


    async readData(){
    let load = this.loading.create({
    content: "Setting up your profile...",
    spinner:'dots'
    });
    load.present();
    await this.fAuth.authState.subscribe((user:firebase.User) =>{
    if(user){
    firebase.database().ref('/users/' +
    user.uid).once('value').then(function(snapshot){
    if(snapshot.exists()){
    var data = (snapshot.val() && snapshot.val().username) ||
    'Anonymous';
    //this.userinfo = data;
    console.log(data);
    }else{
    console.log("i need a name");
    }});
    }else{
    console.log("not logged in, log in please");
    this.alertLogin();
    };
    });
    console.log(this.userinfo );
    load.dismiss();
    }


    getName(){
    let alert = this.alertCtrl.create({
    title: 'Hello new friend! :) please can you tell us your name...',
    inputs: [
    {
    name: 'name',
    placeholder: 'name'
    }
    ],
    buttons: [
    {
    text: 'Cancel',
    role: 'cancel',
    handler: data => {
    console.log('Cancel clicked');
    this.navCtrl.setRoot(MenuPage);
    }
    },
    {
    text: 'Confirm',
    handler: data => {
    this.fAuth.authState.subscribe((user:firebase.User) =>{
    firebase.database().ref('/users/' + user.uid).set({
    username:data.name
    });
    });
    }
    }
    ]
    });
    alert.present();
    }

    alertLogin(){
    //if user is not already logged in
    let alert = this.alertCtrl.create({
    title: 'Whoa there Sally! you need to log in first! :)',
    inputs: [
    {
    name: 'email',
    placeholder: 'email'
    },
    {
    name: 'password',
    placeholder: 'Password',
    type: 'password'
    }
    ],
    buttons: [
    {
    text: 'Cancel',
    role: 'cancel',
    handler: data => {
    console.log('Cancel clicked');
    this.navCtrl.setRoot(MenuPage);
    }
    },
    {
    text: 'Login',
    handler: data => {
    this.login(data.email,data.password);
    }
    },{
    text: 'Register',
    handler: data => {
    this.register(data.email,data.password);
    }
    }
    ]
    });
    alert.present();
    }

    async login(email,password){
    try{
    var login = await this.fAuth.auth.signInWithEmailAndPassword(
    email,
    password
    );
    if(login){
    console.log("Successfully logged in!");
    }
    }catch(err){
    console.error(err);
    alert("Sorry we couldnt find you in our system :(");
    this.navCtrl.setRoot(MenuPage);
    }
    }

    async register(email,password){
    try{
    var reg = await this.fAuth.auth.createUserWithEmailAndPassword(
    email,
    password
    );
    if(reg){
    this.getName();
    console.log("successfully registered!");
    this.navCtrl.setRoot(AccountPage);
    }
    }catch(err){
    console.error(err);
    }
    }

    logout(){
    this.fAuth.auth.signOut();
    }

    }


    .html file:



    <ion-item>
    <h1>{{userinfo}}</h1>
    </ion-item>









    share|improve this question

























      1












      1








      1








      Okay so i am fairly new at ionic and i am experiencing this problem where by i am getting the users data from firebase but whenever i set it to the public variable and try and reference it in the html file, i am getting a log error of "cannot set 'variable name' to property of undefined". Here is my code for a more clearer explanation and understanding of what i am trying to achieve. Thank you.



      .ts file:



      import { Component } from '@angular/core';
      import { IonicPage, NavController, NavParams,AlertController,
      LoadingController, Loading } from 'ionic-angular';
      import {AngularFireAuth} from 'angularfire2/auth';
      import { MenuPage } from '../menu/menu';
      import firebase from 'firebase';
      import { first } from 'rxjs/operators';



      @IonicPage()
      @Component({
      selector: 'page-account',
      templateUrl: 'account.html',
      })
      export class AccountPage {

      public userinfo;

      constructor(public navCtrl: NavController, public navParams: NavParams,
      private alertCtrl:AlertController, public fAuth:AngularFireAuth,
      public loading:LoadingController) {
      }
      ionViewDidLoad() {
      console.log('ionViewDidLoad AccountPage');
      this.readData();
      }

      isLoggedIn(){
      return this.fAuth.authState.pipe(first()).toPromise();
      }

      userStatus(){
      const user = this.isLoggedIn()
      if(user){
      console.log('logged in');
      this.readData();
      }else{
      }
      }


      async readData(){
      let load = this.loading.create({
      content: "Setting up your profile...",
      spinner:'dots'
      });
      load.present();
      await this.fAuth.authState.subscribe((user:firebase.User) =>{
      if(user){
      firebase.database().ref('/users/' +
      user.uid).once('value').then(function(snapshot){
      if(snapshot.exists()){
      var data = (snapshot.val() && snapshot.val().username) ||
      'Anonymous';
      //this.userinfo = data;
      console.log(data);
      }else{
      console.log("i need a name");
      }});
      }else{
      console.log("not logged in, log in please");
      this.alertLogin();
      };
      });
      console.log(this.userinfo );
      load.dismiss();
      }


      getName(){
      let alert = this.alertCtrl.create({
      title: 'Hello new friend! :) please can you tell us your name...',
      inputs: [
      {
      name: 'name',
      placeholder: 'name'
      }
      ],
      buttons: [
      {
      text: 'Cancel',
      role: 'cancel',
      handler: data => {
      console.log('Cancel clicked');
      this.navCtrl.setRoot(MenuPage);
      }
      },
      {
      text: 'Confirm',
      handler: data => {
      this.fAuth.authState.subscribe((user:firebase.User) =>{
      firebase.database().ref('/users/' + user.uid).set({
      username:data.name
      });
      });
      }
      }
      ]
      });
      alert.present();
      }

      alertLogin(){
      //if user is not already logged in
      let alert = this.alertCtrl.create({
      title: 'Whoa there Sally! you need to log in first! :)',
      inputs: [
      {
      name: 'email',
      placeholder: 'email'
      },
      {
      name: 'password',
      placeholder: 'Password',
      type: 'password'
      }
      ],
      buttons: [
      {
      text: 'Cancel',
      role: 'cancel',
      handler: data => {
      console.log('Cancel clicked');
      this.navCtrl.setRoot(MenuPage);
      }
      },
      {
      text: 'Login',
      handler: data => {
      this.login(data.email,data.password);
      }
      },{
      text: 'Register',
      handler: data => {
      this.register(data.email,data.password);
      }
      }
      ]
      });
      alert.present();
      }

      async login(email,password){
      try{
      var login = await this.fAuth.auth.signInWithEmailAndPassword(
      email,
      password
      );
      if(login){
      console.log("Successfully logged in!");
      }
      }catch(err){
      console.error(err);
      alert("Sorry we couldnt find you in our system :(");
      this.navCtrl.setRoot(MenuPage);
      }
      }

      async register(email,password){
      try{
      var reg = await this.fAuth.auth.createUserWithEmailAndPassword(
      email,
      password
      );
      if(reg){
      this.getName();
      console.log("successfully registered!");
      this.navCtrl.setRoot(AccountPage);
      }
      }catch(err){
      console.error(err);
      }
      }

      logout(){
      this.fAuth.auth.signOut();
      }

      }


      .html file:



      <ion-item>
      <h1>{{userinfo}}</h1>
      </ion-item>









      share|improve this question














      Okay so i am fairly new at ionic and i am experiencing this problem where by i am getting the users data from firebase but whenever i set it to the public variable and try and reference it in the html file, i am getting a log error of "cannot set 'variable name' to property of undefined". Here is my code for a more clearer explanation and understanding of what i am trying to achieve. Thank you.



      .ts file:



      import { Component } from '@angular/core';
      import { IonicPage, NavController, NavParams,AlertController,
      LoadingController, Loading } from 'ionic-angular';
      import {AngularFireAuth} from 'angularfire2/auth';
      import { MenuPage } from '../menu/menu';
      import firebase from 'firebase';
      import { first } from 'rxjs/operators';



      @IonicPage()
      @Component({
      selector: 'page-account',
      templateUrl: 'account.html',
      })
      export class AccountPage {

      public userinfo;

      constructor(public navCtrl: NavController, public navParams: NavParams,
      private alertCtrl:AlertController, public fAuth:AngularFireAuth,
      public loading:LoadingController) {
      }
      ionViewDidLoad() {
      console.log('ionViewDidLoad AccountPage');
      this.readData();
      }

      isLoggedIn(){
      return this.fAuth.authState.pipe(first()).toPromise();
      }

      userStatus(){
      const user = this.isLoggedIn()
      if(user){
      console.log('logged in');
      this.readData();
      }else{
      }
      }


      async readData(){
      let load = this.loading.create({
      content: "Setting up your profile...",
      spinner:'dots'
      });
      load.present();
      await this.fAuth.authState.subscribe((user:firebase.User) =>{
      if(user){
      firebase.database().ref('/users/' +
      user.uid).once('value').then(function(snapshot){
      if(snapshot.exists()){
      var data = (snapshot.val() && snapshot.val().username) ||
      'Anonymous';
      //this.userinfo = data;
      console.log(data);
      }else{
      console.log("i need a name");
      }});
      }else{
      console.log("not logged in, log in please");
      this.alertLogin();
      };
      });
      console.log(this.userinfo );
      load.dismiss();
      }


      getName(){
      let alert = this.alertCtrl.create({
      title: 'Hello new friend! :) please can you tell us your name...',
      inputs: [
      {
      name: 'name',
      placeholder: 'name'
      }
      ],
      buttons: [
      {
      text: 'Cancel',
      role: 'cancel',
      handler: data => {
      console.log('Cancel clicked');
      this.navCtrl.setRoot(MenuPage);
      }
      },
      {
      text: 'Confirm',
      handler: data => {
      this.fAuth.authState.subscribe((user:firebase.User) =>{
      firebase.database().ref('/users/' + user.uid).set({
      username:data.name
      });
      });
      }
      }
      ]
      });
      alert.present();
      }

      alertLogin(){
      //if user is not already logged in
      let alert = this.alertCtrl.create({
      title: 'Whoa there Sally! you need to log in first! :)',
      inputs: [
      {
      name: 'email',
      placeholder: 'email'
      },
      {
      name: 'password',
      placeholder: 'Password',
      type: 'password'
      }
      ],
      buttons: [
      {
      text: 'Cancel',
      role: 'cancel',
      handler: data => {
      console.log('Cancel clicked');
      this.navCtrl.setRoot(MenuPage);
      }
      },
      {
      text: 'Login',
      handler: data => {
      this.login(data.email,data.password);
      }
      },{
      text: 'Register',
      handler: data => {
      this.register(data.email,data.password);
      }
      }
      ]
      });
      alert.present();
      }

      async login(email,password){
      try{
      var login = await this.fAuth.auth.signInWithEmailAndPassword(
      email,
      password
      );
      if(login){
      console.log("Successfully logged in!");
      }
      }catch(err){
      console.error(err);
      alert("Sorry we couldnt find you in our system :(");
      this.navCtrl.setRoot(MenuPage);
      }
      }

      async register(email,password){
      try{
      var reg = await this.fAuth.auth.createUserWithEmailAndPassword(
      email,
      password
      );
      if(reg){
      this.getName();
      console.log("successfully registered!");
      this.navCtrl.setRoot(AccountPage);
      }
      }catch(err){
      console.error(err);
      }
      }

      logout(){
      this.fAuth.auth.signOut();
      }

      }


      .html file:



      <ion-item>
      <h1>{{userinfo}}</h1>
      </ion-item>






      firebase ionic3






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 8:22









      Lady_ALady_A

      157




      157
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Okay so i have tried basically everything and have decided to rather just upload the name to the database and write it locally in the storage of the application. I will just pull the name, along with further details later on in the application process. However if anyone finds an answer for this post, i would greatly appreciate it!






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985182%2fsetting-firebase-data-to-property-in-html-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Okay so i have tried basically everything and have decided to rather just upload the name to the database and write it locally in the storage of the application. I will just pull the name, along with further details later on in the application process. However if anyone finds an answer for this post, i would greatly appreciate it!






            share|improve this answer




























              0














              Okay so i have tried basically everything and have decided to rather just upload the name to the database and write it locally in the storage of the application. I will just pull the name, along with further details later on in the application process. However if anyone finds an answer for this post, i would greatly appreciate it!






              share|improve this answer


























                0












                0








                0







                Okay so i have tried basically everything and have decided to rather just upload the name to the database and write it locally in the storage of the application. I will just pull the name, along with further details later on in the application process. However if anyone finds an answer for this post, i would greatly appreciate it!






                share|improve this answer













                Okay so i have tried basically everything and have decided to rather just upload the name to the database and write it locally in the storage of the application. I will just pull the name, along with further details later on in the application process. However if anyone finds an answer for this post, i would greatly appreciate it!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 31 '18 at 9:32









                Lady_ALady_A

                157




                157






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985182%2fsetting-firebase-data-to-property-in-html-file%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas