2013-07-02 31 views
1

新角和firebase。我在初始頁面加載時遇到了「TypeError:Can not call method'child'of undefined」的錯誤。它尚未定義,因爲它尚未準備好,但我不確定如何處理這種情況。TypeError:無法調用未定義的方法'child'

方案如下: 2.火力地堡 1.用戶登錄驗證用戶登錄 3.用戶有孩子的朋友列表「朋友」 4.代碼試圖火力地堡之前得到孩子的朋友可以驗證用戶

的代碼登錄

$rootScope.userRef = $rootScope.fireRef.child('users'); 
    $rootScope.authClient = new FirebaseAuthClient($rootScope.fireRef, function(error, user) { 
    if (error) { 
     // an error occurred while attempting login 
     console.log(error); 
    } else if (user) { 
     // user authenticated with Firebase    
     $rootScope.userData = $rootScope.userRef.child(user.id); 
    } else { 
     // user is logged out    
    } 
}); 

後來,在FriendsController當用戶在/朋友航線

var friendsList = $scope.userData.child('friends'); 
    var promise = angularFire(friendsList.limit(10), $scope, 'friends', {}); 

這個錯誤發生在friendsList上,因爲userData ref沒有準備好(我認爲)。從其他地方導航到/朋友不會遇到此問題,僅在網頁加載時纔有。我正在尋找解決方案來處理這種情況。謝謝。

編輯:其他建議我嘗試過基於迄今爲止從加藤和阿南特提出的建議。在朋友控制器

var def = $q.defer(); 
    $rootScope.loginPromise = def.promise; 
    // then in authclient 
    def.resolve(); 

然後:

使用承諾

$rootScope.loginPromise.then(function() { 
     var friendsList = $scope.userData.child('friends'); 
     var promise = angularFire(friendsList.limit(10), $scope, 'friends', {}); 
    }); 

這導致在好友列表中不加載。有趣的是,當我點擊一個菜單按鈕,然後loginPromise運行。這不是我所期望的。任何想法,爲什麼這可能是?

使用觀察者模式:

$rootScope.broadcast('authenticated') // in user verification 

在FriendsController:

$rootScope.$on('authenticated', function() { 
     var friendsList = $scope.userData.child('friends'); 
     var promise = angularFire(friendsList.limit(10), $scope, 'friends', {}); 
    }); 

此工程在頁面加載,但如果瀏覽的頁面,因爲不再有的「認證的賽事轉播」。所以這似乎引出了另一個問題。如何導航到/好友,以便加載所需的數據?

回答

2

您需要等到authClient的回調被觸發時,纔會懷疑。這個問題有幾種解決方案。

設置一個承諾

由於角不會介意$ scope.friends是不確定的,最簡單的答案可能是簡單地在authClient use a promise和參考之前設定的朋友:

var def = $q.defer(); 
$rootScope.loginPromise = def.promise(); 
$rootScope.authClient = new FirebaseAuthClient($rootScope.fireRef, function(error, user) { 
    if (error) { 
     // an error occurred while attempting login 
     console.log(error); 
    } else if (user) { 
     // user authenticated with Firebase    
     $rootScope.userData = $rootScope.userRef.child(user.id); 
     def.resolve(); 
    } else { 
     // user is logged out    
    } 
}); 

// FriendsController 
$rootScope.loginPromise.then(function() { 
    var friendsList = $scope.userData.child('friends'); 
    var promise = angularFire(friendsList.limit(10), $scope, 'friends', {}); 
}); 

使用觀察者模式

另一種解決方案是使用$ rootScope。$廣播,提醒您對照當auth完成時,然後你FriendsController可以監聽事件,並做相應的工作:

$rootScope.$on('authenticated', function() { 
    var friendsList = $scope.userData.child('friends'); 
    var promise = angularFire(friendsList.limit(10), $scope, 'friends', {}); 
}); 

摘要認證

通常你可以簡單地在路徑級處理身份驗證,而不必在你的控制器來處理它(確保auth發生之前,他們可以達到安全的數據)。

使用庫

退房的angularFire repo所有這些更多的例子和一個不錯的lib,可以抽象的大部分工作。

+0

我還建議您查看添加到angularFire的新身份驗證支持,以下是有關如何在此處使用它的概述:https://groups.google.com/d/msg/firebase-talk/D6wgBgUtK94/AygqI4oJ7b4J – Anant

+0

Hi @Kato,感謝您的建議。我遇到了一些有關promise的問題並使用了觀察者模型,並用我的結果編輯了我的答案。也許你可以幫助我。我也無法獲得angularFire身份驗證,但我會繼續修補,因爲我學到了很多東西。謝謝。 – landland

+0

@Anant按照谷歌組線程中的說明,我能夠獲得身份驗證。感謝他們非常有幫助的指示。 – landland

相關問題