其给定的未定义。无法从Firebase检索名称

我想在登录后从Firebase获取用户数据。当我从数据库中获取名称时,它提供的值未定义。为什么?

var user = firebase.auth().currentUser;

if(user != null ){

  var email_id = user.email;
  var uid = user.uid;
  var name = user.userName; 
  var user = user;

  document.getElementById("user_para").innerHTML = "Welcome User : " + email_id;
  document.getElementById("user_para1").innerHTML = "Welcome User : " + uid;
  document.getElementById("user_para2").innerHTML = "Welcome User : " + name;


}
回答如下:

请记住,firebase身份验证仅保存以下数据:

email: '[email protected]',
emailVerified: false,
phoneNumber: '+11234567890',
password: 'secretPassword',
displayName: 'John Doe',
photoURL: 'http://www.example/12345678/photo.png',
disabled: false

因此user.userName不存在。

您可以使用displayName保存用户名,但是如果您真的使用它来保存名称,则可以在firebase实时数据库中创建一个称为用户的节点,您可以在其中基于UID保存用户名,如下所示:] >

users
   ->akdjf231dkeimdla
         ->username: jamesbond007

您编写的代码可能是:

var user = firebase.auth().currentUser;

if(user != null ){

 var email_id = user.email;
 var uid = user.uid;
 var name = user.displayName;

 document.getElementById("user_para").innerHTML = "Welcome User : " + email_id;
 document.getElementById("user_para1").innerHTML = "Welcome User : " + uid;
 document.getElementById("user_para2").innerHTML = "Welcome User : " + name;

 firebase.database().ref('/users/' + uid).once('value').then(function(snapshot) {
   //Here are reading the username from the database
   var username = snapshot.val().username;

 });

}

如果使用Firebase实时数据库,请记住添加Firebase实时数据库库。

有关更多信息:https://firebase.google/docs/auth/admin/manage-users

其给定的未定义。无法从Firebase检索名称

我想在登录后从Firebase获取用户数据。当我从数据库中获取名称时,它提供的值未定义。为什么?

var user = firebase.auth().currentUser;

if(user != null ){

  var email_id = user.email;
  var uid = user.uid;
  var name = user.userName; 
  var user = user;

  document.getElementById("user_para").innerHTML = "Welcome User : " + email_id;
  document.getElementById("user_para1").innerHTML = "Welcome User : " + uid;
  document.getElementById("user_para2").innerHTML = "Welcome User : " + name;


}
回答如下:

请记住,firebase身份验证仅保存以下数据:

email: '[email protected]',
emailVerified: false,
phoneNumber: '+11234567890',
password: 'secretPassword',
displayName: 'John Doe',
photoURL: 'http://www.example/12345678/photo.png',
disabled: false

因此user.userName不存在。

您可以使用displayName保存用户名,但是如果您真的使用它来保存名称,则可以在firebase实时数据库中创建一个称为用户的节点,您可以在其中基于UID保存用户名,如下所示:] >

users
   ->akdjf231dkeimdla
         ->username: jamesbond007

您编写的代码可能是:

var user = firebase.auth().currentUser;

if(user != null ){

 var email_id = user.email;
 var uid = user.uid;
 var name = user.displayName;

 document.getElementById("user_para").innerHTML = "Welcome User : " + email_id;
 document.getElementById("user_para1").innerHTML = "Welcome User : " + uid;
 document.getElementById("user_para2").innerHTML = "Welcome User : " + name;

 firebase.database().ref('/users/' + uid).once('value').then(function(snapshot) {
   //Here are reading the username from the database
   var username = snapshot.val().username;

 });

}

如果使用Firebase实时数据库,请记住添加Firebase实时数据库库。

有关更多信息:https://firebase.google/docs/auth/admin/manage-users