【问题标题】:Uncaught TypeError: Cannot read property 'uid' of null [duplicate]未捕获的类型错误:无法读取 null 的属性“uid”[重复]
【发布时间】:2019-12-01 19:31:34
【问题描述】:

我不知道为什么 UID 返回 null。 userSessionCheck() 包含登录后保持会话的代码。 user.uid 给出相同的结果下面是我的代码和错误代码

--

未捕获的类型错误:无法读取 null 的属性“uid”

  $(document).ready(function(){
  auth = firebase.auth();
  db = firebase.database();
  storage = firebase.storage();
  userInfo = auth.currentUser;


  userSessionCheck();
  update_profile();


  function update_profile() {

    var mypage_Ref = db.ref('users/' + userInfo.uid).once('value').then(function(snapshot){
      console.log(snapshot.val());
      profileImageUrl = $('#update_email').val(snapshot.val().profileImageUrl);
      email = $('#update_email').val(snapshot.val().email);
      u_name = $('#update_name').val(snapshot.val().username);
      job = $('#update_job').val(snapshot.val().job);


      var html =
      '<div class="profile">' +
       '<img src="'+profileImageUrl+'" id="profile_img">'+

       '<div class="form_g">'+
         '<label>email</label>'+
        '<input type="text" style="text-align: center" id="update_email" placeholder="'+ email +'">'+
       '</div>'+
       '<div class="form_g">'+
         '<label>name</label>'+
         '<input type="text" style="text-align: center" id="update_name" placeholder="'+u_name+'">'+
       '</div>'+
       '<div class="form_g">'+
         '<label>job</label>'+
         '<h5 id="update_job">'+job+'</h5>'+
       '</div>'+
       '<button type="button" class="s-btn" id="info_submit" style="width: 100%; margin-top: 30px;">update</button>'+
      '</div>';

      $('#profile_form').append(html);


    });
  }
});

【问题讨论】:

  • 在尝试访问其中的任何变量之前尝试console.log(userInfo),看看它是否是null
  • 可能 html 甚至在检索数据之前就尝试使用数据。在附加模板之前,您需要检查它是否为空。

标签: javascript firebase firebase-authentication


【解决方案1】:

userInfo = auth.currentUser 给你 null,这意味着userInfo.uid 会给出那个错误。当auth.currentUser 为空时,表示没有用户登录。在新页面加载时,用户不会立即登录,因此您应该使用auth state observer 来确定登录用户帐户何时首次可用。

【讨论】:

    猜你喜欢
    • 2019-10-19
    • 2018-07-11
    • 1970-01-01
    • 2021-11-18
    • 2022-06-13
    • 2012-08-01
    • 1970-01-01
    • 2022-01-19
    • 2021-12-01
    相关资源
    最近更新 更多