2

I need to get a snapshot of an item from Firebase so that I can use it later in a path. I have validation to prevent null values from being entered. Before using this snapshot, my validation was working fine. However, putting this snapshot in ionViewDidLoad and the constructor have caused the validation to fire when the page first loads and also correctly upon a submit attempt. How can I fix this issue?

Here is the Typescript:

export class CustomItemPage {

  user: {};
  items: FirebaseListObservable<any[]>;
  customitemname: string = '';
  customitemcat: string = '';
  listKey = "";

ionViewDidLoad(){
  firebase.auth().onAuthStateChanged((user)=> {
    this.user = user;
    console.log('authStateINVITE',user);
    if (user) {
      // User is signed in.
      var displayName = user.displayName;
      var email = user.email;
      var emailVerified = user.emailVerified;
      var photoURL = user.photoURL;
      var isAnonymous = user.isAnonymous;
      var uid = user.uid;
      var providerData = user.providerData;
      // this.navCtrl.setRoot(TutorialPage);
      // ...
    } else {
      // User is signed out.
      // ...
    }
  });

  var user = firebase.auth().currentUser;
  var uid = user.uid;

  firebase.database().ref('/userprofile/' + uid + '/' + 'list').once('value').then((snapshot)=> {
    var listkey = (snapshot.val());
    console.log(listkey)
    this.listKey = listkey;
    this.addcustomitem();//, listkey);
  });

}

  constructor(public navCtrl: NavController, private afAuth: AngularFireAuth, public af: AngularFireDatabase, public alerCtrl: AlertController, private toastCtrl: ToastController) {
    this.items = af.list('/messages', {
          query: {
            limitToLast: 50
          }
        });

        //this.user = this.afAuth.authState;
    }

        logout() {
            this.afAuth.auth.signOut();
            this.navCtrl.setRoot(SignInPage)
        }

        addcustomitem() {
            var listkey = this.listKey;
            var customitemname = this.customitemname;
            var customitemcat = this.customitemcat

            var customlistData = {
              category: customitemcat,
              foodname: customitemname,
              state: "on",
            };

            var user = firebase.auth().currentUser;
            var uid = user.uid;

            var foodid = firebase.database().ref().child('posts').push().key;

            if (customitemname==null || customitemname=="")
              {
                let alert = this.alerCtrl.create({
                      title: 'Error!',
                      message: 'Please enter a name for your item.',
                      buttons: ['Ok']
                    });
                    alert.present()
                  }
            else if (customitemcat==null || customitemcat=="")
              {
                let alert = this.alerCtrl.create({
                      title: 'Error!',
                      message: 'Please enter a category for your item.',
                      buttons: ['Ok']
                    });
                    alert.present()
                  }
            else{
                this.navCtrl.pop(); //SENDS TO PREVIOUS PAGE

                let toast = this.toastCtrl.create({
                  message: customitemname + ' was added successfully',
                  duration: 3000,
                  position: 'bottom'
                });

                toast.onDidDismiss(() => {
                  console.log('Dismissed toast');
                });

                toast.present();

                return firebase.database().ref('userlists' + '/' + listkey +  '/' + 'list' + '/' + foodid).update(customlistData);
                }
        }

}

1 Answer 1

0

I was able to fix this by removing:

this.addcustomitem();//, listkey);

From ionViewDidLoad().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.