检索 Firebase 快照导致加载验证错误

Posted

技术标签:

【中文标题】检索 Firebase 快照导致加载验证错误【英文标题】:Retrieving a Firebase snapshot is causing validation errors onload 【发布时间】: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);
                
        


【问题讨论】:

【参考方案1】:

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

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

来自ionViewDidLoad()

【讨论】:

以上是关于检索 Firebase 快照导致加载验证错误的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Firebase:状态不佳:快照既没有数据也没有错误

密码注册后 Firebase 云功能出现未经身份验证的错误

swift - Firebase 数据库快照检索。

Swift不检索Firebase值(返回值为NSNull)

Firebase 实时数据库值检索

Firebase-admin:- 部署失败:错误:您的 requirements.txt 无效。快照您的日志以获取详细信息。