未捕获的类型错误

未捕获的类型错误

本文介绍了未捕获的类型错误:无法使用网站读取 null 的属性“uid"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将注册用户的详细信息从 firestore 获取到 html 页面中.当用户单击我的个人资料页面时,它会导航到下一个 html 页面,其中包含名字和姓氏等字段.此字段已在注册页面时输入.所以我想从 firestore 获取这些详细信息到下一个 html 页面.但是我收到了无法读取 null 的属性 'uid'"之类的错误.如何解决这个问题,这是我的 html 页面:

<html lang="zh-cn"><头>deyaPay</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1,shrink-to-fit=no"><script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-app.js"></script><script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-auth.js"></script><script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script><script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-firestore.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszubwgT"crossori><脚本>变量配置 = {apiKey: "AizaSyAJsAstiMsrB2QGJyoqiTELw0vsWFVVahw",authDomain: "websignin-79fec.firebaseapp.com",databaseURL: "https://websignin-79fec.firebaseio.com",projectId: "websignin-79fec",存储桶:"",messagesSenderId:480214472501"};firebase.initializeApp(config);<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-firestore.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszubwgT"crossori><风格>//css 在这里..</风格><script type="text/javascript">函数 myProfile() {var user = firebase.auth().currentUser.uid;var db = firebase.firestore();var docRef = db.collection("deyaPayusers").doc(user);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(函数(错误){console.log("出现错误:",error);});}<body onload='myProfile()'><div class="登录表单"><form method="post"><h2>个人资料</h2><div class="form-group"><input type="text" name="firstname" id="fnmae" placeholder="FirstName" class="form-control" >

<div class="form-group"><input type="text" name="lastname" id="lnmae" placeholder="LastName" class="form-control" >

<div class="form-group"><input type="text" name="phone" id="phone" placeholder="PhoneNumber" class="form-control" >

</表单>

解决方案

你的错误是Cannot read property 'uid' of null.这意味着您在页面加载时加载 myProfile() 这个函数,那时 user 可能为空.从 firebase 获取身份验证详细信息会有延迟.试试这个方法

firebase.auth().onAuthStateChanged(function(user) {如果(用户){//用户已登录.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(函数(错误){console.log("出现错误:",error);});} 别的 {//没有用户登录.}});

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 etc.. . This fields are already entered at the time of signup page. so i want get those details from firestore into next html page. But i got the error like "Cannot read property 'uid' of null ". How to resolve that problem this is my html page :

<!doctype html>
<html lang="en">
    <head>
        <title> deyaPay</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
         <script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-app.js"></script>
         <script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-auth.js"></script>
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
        <script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-firestore.js"></script>
         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
         <script>
          var config = {
    apiKey: "AIzaSyAJsAstiMsrB2QGJyoqiTELw0vsWFVVahw",
    authDomain: "websignin-79fec.firebaseapp.com",
    databaseURL: "https://websignin-79fec.firebaseio.com",
    projectId: "websignin-79fec",
    storageBucket: "",
    messagingSenderId: "480214472501"
  };
   firebase.initializeApp(config);
   </script>
   <script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-firestore.js"></script>
         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
   <style>
   // css goes here..
   </style>
   <script type="text/javascript">
      function myProfile() {
          var user = firebase.auth().currentUser.uid;
          var db = firebase.firestore();
          var docRef = db.collection("deyaPayusers").doc(user);
          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);
        });
      }

    </script>
   </head>
   <body onload='myProfile()'>
      <div class= "login-form">
          <form method="post">
             <h2>Profile</h2>
              <div class="form-group">
                  <input type="text" name="firstname" id="fnmae" placeholder="FirstName" class="form-control" >
              </div>
               <div class="form-group">
                  <input type="text" name="lastname" id="lnmae" placeholder="LastName" class="form-control" >
              </div>
              <div class="form-group">
                 <input type="text" name="phone" id="phone" placeholder="PhoneNumber" class="form-control" >
              </div>

          </form>
      </div>

   </body>
 </html>
解决方案

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

这篇关于未捕获的类型错误:无法使用网站读取 null 的属性“uid"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 09:11