【问题标题】:How can I store User data to my User node using AngularFir?如何使用 AngularFir 将用户数据存储到我的用户节点?
【发布时间】:2016-05-13 23:24:17
【问题描述】:

我已经创建了我的登录名,并且现在能够允许用户注册。但是我想将用户数据存储到我的节点。我添加了我认为应该在 addUser 函数中工作的代码。我已经为此工作了很长时间,但找不到解决方案。有人可以指出我做错了什么的正确方向吗?

我写的代码如下。提前致谢。

'use strict';

var theBigWeddingBook = angular.module('theBigWeddingBook');

theBigWeddingBook.controller('RegCtrl', function ($scope, $firebaseAuth,   $firebaseObject)

{
  var ref = new Firebase("https://the-big-wedding-book.firebaseio.com");
  var reg = $firebaseAuth(ref);

$scope.user = {};

$scope.addUser = function() {
    var username = $scope.user.email;
    var password = $scope.user.password;
    var uid = $scope.user.uid;

    reg.$createUser({
        email: username,
        password: password
    }).then(function(user) {
        console.log('User has been successfully created!');
    }).catch(function(error) {
        console.log(error);
    })

    ref.child('user').child(user.uid).set(user).then(function(user) {
        console.log('Data Saved Successfully!');
    }).catch(function(error) {
        console.log(error);
    })
};
})

【问题讨论】:

    标签: angularjs firebase angularfire firebase-realtime-database firebase-authentication


    【解决方案1】:
    $scope.addUser = function() {
    
      var uid = $scope.user.uid;
    
      reg.$createUser({
        email: $scope.user.email,
        password: $scope.user.password
      }).then(function(user) {
         console.log('User has been successfully created!');
      }).catch(function(error) {
        console.log(error);
      })
    })
    

      $scope.addUser = function() {
        var username = $scope.user.email;
        var password = $scope.user.password;
        var uid = $scope.user.uid;
    
        reg.$createUser({
            email: this.username,
            password: this.password
        }).bind(this)
          .then(function(user) {
            console.log('User has been successfully created!');
        }).catch(function(error) {
            console.log(error);
        })
    

    【讨论】:

    • 这不会将 UID 和 Provider 保存到我在 firebase JSON 中创建的自定义用户节点中吗?
    • 嗯,您发布的原始代码中没有捕捉到这一点。所以我没有这样做,但逻辑保持不变。
    【解决方案2】:

    我让它工作了。我基本上改变了它,以便当有人注册时他们会自动登录,然后在身份验证时将数据保存到用户节点,如下所示。

    var theBigWeddingBook = angular.module('theBigWeddingBook');
    
    theBigWeddingBook.controller('RegCtrl', function ($scope, $firebaseAuth, $firebaseObject) {
    
        var ref = new Firebase("https://the-big-wedding-book.firebaseio.com");
        var reg = $firebaseAuth(ref);
    
        ref.onAuth(function(authData) {
          if (authData) {
            console.log("Authenticated with uid:", authData.uid);
          } else {
            console.log("Client unauthenticated.")
          }
        });
    
        $scope.user = {};
    
        $scope.addUser = function() {
            var username = $scope.user.email;
            var password = $scope.user.password;
            var uid = $scope.user.uid;
    
            reg.$createUser({
                email: username,
                password: password
            }).then(function(user) {
                console.log('User has been successfully created!');
                return reg.$authWithPassword({ email: username, password: password });
            }).catch(function(error) {
                console.log(error);
            })
        };
    
        ref.onAuth(function(authData) {
            ref.child('user').child(authData.uid).set(authData).then(function(auth) {
                console.log('Data Saved Successfully!');
            }).catch(function(error) {
                console.log(error);
            })
        })
    
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2018-06-30
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2016-06-10
      相关资源
      最近更新 更多