0

我創建了我的登錄名,並且已經允許用戶現在註冊。不過,我想將用戶數據存儲到我的節點。我添加了我認爲應該在addUser函數內工作的代碼。我已經在這方面工作了很多年,但找不到解決方案。有人能指出我正確的方向嗎?我做錯了什麼?如何使用AngularFir將用戶數據存儲到我的用戶節點?

我寫的代碼如下。提前致謝。

'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); 
    }) 
}; 
}) 

回答

0

我得到它的工作。我基本上改變它,這樣當有人註冊他們自動登錄,然後進行身份驗證時,數據將保存到用戶節點,如下所示。

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); 
     }) 
    }) 

}) 
0
$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); 
    }) 
}) 

OR

$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); 
    }) 
+0

這不會將UID和提供程序保存到我在Firebase JSON中創建的自定義用戶節點嗎? –

+0

那麼這並不是在您發佈的原始代碼中捕獲的。所以我沒有這樣做,但邏輯保持不變。 – shiv

相關問題