Uncaught TypeError: Cannot read property 'uid' of null using website

前端 未结 1 471
孤街浪徒
孤街浪徒 2020-12-18 06:20

I want to get registered user details from firestore into html page. When user clicks my profile page it navigate to next html page with fields like first name and last name

1条回答
  •  悲哀的现实
    2020-12-18 06:53

    Your error says Cannot read property 'uid' of null. which means you are loading myProfile() this function on page load, by that time user may be null. There will be a lag to fetch auth details from firebase. Try this way

    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        // User is signed in.
        var db = firebase.firestore();
          var docRef = db.collection("deyaPayusers").doc(user.uid);
          docRef.get().then(function(doc) {
             if(doc && doc.exists) {
             const myData = doc.data();
             const ffname = myData.FirstName;
             const llname = myData.LastName;
             const phonen = myData.PhoneNumber;
             document.getElementById("fname").value = ffname;
             document.getElementById("lname").value = llname;
             document.getElementById("phone").value = phonen;
    
        }
        }).catch(function(error) {
        console.log("Got an error: ",error);
        });
      } else {
        // No user is signed in.
      }
    });
    

    0 讨论(0)
提交回复
热议问题