将创建的用户链接到Firebase Web应用程序中的数据库(linking created users to database in firebase web app)

编程入门 行业动态 更新时间:2024-10-12 14:21:53
将创建的用户链接到Firebase Web应用程序中的数据库(linking created users to database in firebase web app)

如何将已通过身份验证的用户链接到我的Angular JS Web应用程序中的Firebase数据库?

我知道我必须在每个用户的数据库树中创建一个节点。 我在我的数据库树中为此创建了一个名为“用户”的节点。 我不明白的是如何让每个用户在我的数据库的“用户”节点中占用一个子节点,以便一旦登录,每个用户保存的所有数据都保存在其各自的子节点下。

这是我目前的代码:

$scope.saveTeam = function(user){ $scope.history = []; var uid = user.uid; var ref2 = firebase.database().ref("users/" + uid + "/week"); ref2.set($scope.history); };

How do I link the authenticated users to my firebase database in my Angular JS web app?

I understand that I would have to create a node in my database tree for each user. I have created a node in my database tree called "users" for this. What I do not understand is how to get each user to occupy a sub-node in my "users" node in my database such that once logged in, all the data each user saves is saved under their respective sub-nodes.

This is my code at the moment:

$scope.saveTeam = function(user){ $scope.history = []; var uid = user.uid; var ref2 = firebase.database().ref("users/" + uid + "/week"); ref2.set($scope.history); };

最满意答案

您使用哪种方法注册用户都会返回一个承诺。

例如,考虑使用电子邮件和密码注册用户:

firebase.auth().createUserWithEmailAndPassword(username, password).then( (user) =>{ let usersRef = firebase.database().ref("users"); //child is created under users node with a user's user id as child name usersRef.child(user.uid).set({ email: user.email, userName: username, displayPicUrl: 'any url.' }); }, error => { //handle errors here } );

因此,您可以在承诺的全部填充部分中执行向数据库中写入新用户的过程,如上所示。 这确保只有在用户注册成功时才创建用户节点

所以在你的代码中添加像这样

$scope.signUp = function (){ var username = $scope.user.email; var password = $scope.user.password; if(username && password){ var auth = $firebaseAuth(); auth.$createUserWithEmailAndPassword(username, password).then(function(user){ console.log("User Successfully Created"); let usersRef = firebase.database().ref("users"); //child is created under users node with a user's user id as child name usersRef.child(user.uid).set({ email: user.email, userName: username, displayPicUrl: 'any url.' }); $location.path('/home'); }).catch(function(error){ $scope.errMsg = true; $scope.errorMessage = error.message; }); } };

then()中没有传入user参数

Which ever method you are using to signup users has a promise returned .

For example consider signing up users with email and password:

firebase.auth().createUserWithEmailAndPassword(username, password).then( (user) =>{ let usersRef = firebase.database().ref("users"); //child is created under users node with a user's user id as child name usersRef.child(user.uid).set({ email: user.email, userName: username, displayPicUrl: 'any url.' }); }, error => { //handle errors here } );

So you can do the process of writing a new user to database in ths promise's fullfilled part as shown above. This ensures that a user node is created only if user sign up was successful

so in your code add like this

$scope.signUp = function (){ var username = $scope.user.email; var password = $scope.user.password; if(username && password){ var auth = $firebaseAuth(); auth.$createUserWithEmailAndPassword(username, password).then(function(user){ console.log("User Successfully Created"); let usersRef = firebase.database().ref("users"); //child is created under users node with a user's user id as child name usersRef.child(user.uid).set({ email: user.email, userName: username, displayPicUrl: 'any url.' }); $location.path('/home'); }).catch(function(error){ $scope.errMsg = true; $scope.errorMessage = error.message; }); } };

You are not passing user parameter in then()

更多推荐

本文发布于:2023-07-30 16:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   链接   数据库   用户   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!