【问题标题】:Retrieving a Firebase snapshot is causing validation errors onload检索 Firebase 快照导致加载验证错误
【发布时间】:2018-05-26 22:13:44
【问题描述】:

我需要从 Firebase 获取项目的快照,以便稍后在路径中使用它。我有验证以防止输入空值。在使用此快照之前,我的验证工作正常。但是,将此快照放入 ionViewDidLoad 和构造函数会导致验证在页面首次加载时触发,并且在提交尝试时也正确。我该如何解决这个问题?

这是打字稿:

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

}

【问题讨论】:

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


    【解决方案1】:

    我可以通过删除来解决这个问题:

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

    来自ionViewDidLoad()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多