firebase.auth().CurrentUser.uid在成功登录后给出null [英] firebase.auth().CurrentUser.uid is giving null after successful login

查看:93
本文介绍了firebase.auth().CurrentUser.uid在成功登录后给出null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了Firebase登录.它已成功登录.我在登录页面个人资料中有四个页面,包括出售,购买和关于我们的信息.当我在配置文件和销售页面中检索当前用户的uid的值时.但是我在购买页面上变得空了,为什么会这样?

I have made use of firebase login. It is logging in successfully. I have four pages inside login page profile, sell, buy and about us. When I am retrieving the value of current user's uid in profile and sell page. But i am getting null in buy page why is it so?

JS 对于用户页面

function save_user()
      {
     const uid = document.getElementById('user_id');
            const userpwd = document.getElementById('user_pwd');
            const btnregister=document.getElementById('btn-action');
            //btnregister.addEventListener('click', e=>
            //{
            const email=uid.value;
            const pass=userpwd.value;
            firebase.auth().signInWithEmailAndPassword(email, pass).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    console.log(error.Message);

});
firebase.auth().onAuthStateChanged(function(user) {
  if(user) {
    window.location='userpage.html'; 
  }
});

JS

用于购买页面,其中firebase.auth().currentUser.uid显示空代码

for buy page where firebase.auth().currentUser.uid is showing null code

var fbRef = firebase.database().ref().child("Sell_Products");
    var user = firebase.auth().currentUser.uid; 
alert(user);    /*Here it's showing null
fbRef.on("child_added", snap => {
    var key=snap.key;
    alert('HEllo');
    alert(key);
    var name = snap.child("name").val();
    var price = snap.child("price").val();
    var category = snap.child("category").val();
    var description = snap.child("description").val();
    var image = snap.child("image").val();
    $("#ex-table").append("<tr><td><a href=\"auction.html?itemKey="+key+"\"><img src=" + image + "/img></a></td><td>" + name + "</td><td>" + price + "</td><td>" + category + "</td><td>" + description + "</td></tr>" );
});

推荐答案

获取登录用户的推荐方法是您在userPage上(使用观察者)处理重定向的方法:

The recommended way to obtain the loggedin user is the way you're handling your redirect on userPage (with an observer):

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    console.log(user);
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

来源: https://firebase.google.com/docs/auth /web/manage-users

因此,您应该具有以下内容:

So you should have something like this:

var fbRef = firebase.database().ref().child("Sell_Products");

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        console.log(user);
        var user = firebase.auth().currentUser.uid; 
        alert(user);    //you should have your user here!
        fbRef.on("child_added", snap => {
            var key=snap.key;
            alert('HEllo');
            alert(key);
            var name = snap.child("name").val();
            var price = snap.child("price").val();
            var category = snap.child("category").val();
            var description = snap.child("description").val();
            var image = snap.child("image").val();
            $("#ex-table").append("<tr><td><a href=\"auction.html?itemKey="+key+"\"><img src=" + image + "/img></a></td><td>" + name + "</td><td>" + price + "</td><td>" + category + "</td><td>" + description + "</td></tr>" );
        });
    } else {
        console.log('No user is signed in.');
    }
});

这篇关于firebase.auth().CurrentUser.uid在成功登录后给出null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆