简体   繁体   English

如何通过注册电子邮件在Firebase中“命名”数据

[英]How to “name” data in firebase from signup email

When storing users in firebase, How do you display the node as their email rather than the characters displayed in the image. 在将用户存储在Firebase中时,如何将节点显示为他们的电子邮件,而不是图像中显示的字符。 I tried a bunch of different things and keep getting errors. 我尝试了一堆不同的方法,并不断出错。 Thank you for your help in advance. 提前谢谢你的帮助。 I appreciate it! 我很感激! :) :)

FireBase Img FireBase图

$scope.signupEmail = function(){  

var ref = new Firebase("https://CoolApp.firebaseio.com");

ref.createUser({
  email    : $scope.data.email,
  password : $scope.data.password
}, function(error, userData) {
  if (error) {
    console.log("Error creating user:", error);
  } else {
    console.log("Successfully created user account with uid:", userData.uid);
    ref.child('users').push(userData); 
    $state.go('tab.home');
  }
});

}; };

See the Firebase documentation on storing user data : 请参阅有关存储用户数据Firebase文档

ref.child("users").child(authData.uid).set({
  provider: authData.provider,
  name: getName(authData)
});

As you'll see, that stores each user under their uid , which is guaranteed to be unique across all providers. 就像您将看到的那样,它将每个用户存储在其uid ,这在所有提供程序中都保证是唯一的。

If you instead want to store the users under their email address, you'll have to do some extra work. 如果您想将用户存储在他们的电子邮件地址下,则必须做一些额外的工作。 Firebase keys cannot contain certain characters (most notable dots: . ) that are present in email addresses, so you'll have to encode the email address. Firebase密钥不能包含电子邮件地址中存在的某些字符(最著名的点: . ),因此您必须对电子邮件地址进行编码。

var encodedEmail = authData.password.email.replace('.',',')
ref.child("users").child(encodedEmail).set({
  provider: authData.provider,
  name: getName(authData)
});

It's most common in cases like this, to actually store the user under their uid (as in the first snippet) and to additionally store a mapping from the (encoded) email address to the uid. 在这种情况下,最常见的是将用户实际存储在其uid (如第一个代码段所示),并另外存储从(编码的)电子邮件地址到uid的映射。 That way you can easily get the user both by their uid and by their email address: 这样,您可以通过用户的uid和电子邮件地址轻松获取用户:

ref.child("users").child(auth.uid).set({
  provider: authData.provider,
  name: getName(authData)
});
var encodedEmail = authData.password.email.replace('.',',')
ref.child("emailMappings").child(encodedEmail).set(auth.uid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM