为什么这个Factory功能不起作用

时间:2016-05-29 23:47:38

标签: javascript angularjs

我已经构建了我认为是一个正常运行的身份验证工厂,但是当我尝试调用Login.auth()时,会抛出一个错误,说它不是一个函数。我有一个不同的工厂/控制器对,完全正常,并遵循与此示例相同的模式。 这是我的代码:
登录工厂:

app.factory('Login', ['$firebaseAuth', function($firebaseAuth) {
var authref = $firebaseAuth(ref);
var authData = {};
return {
    auth: function() {
        if (!authData) {
            authref.$authWithOAuthPopup("google", {
                scope: "email"
            }).then(function(data) {
                //check if account is made, if not create a new account and pass on.
                checkIfUserExists(data);
                authdata = {
                    user: {
                        name: data.google.displayName,
                        pic: data.google.profileImageURL
                    },
                    token: data.token,
                    provider: data.provider,
                    uid: data.uid
                };
                return authData;
            });
        }
        else {
            return authData;
        }
    },
    unAuth: function() {
        ref.unauth();
        authData = null;
        return authData;
    }
}
// Tests to see if /users/<userId> has any data. 
function checkIfUserExists(data) {
    usersRef.child(data.uid).once('value', function(snapshot) {
        var exists = (snapshot.val() !== null);
        userExistsCallback(data, exists); //send the data and the boolean to userExistsCallback.
    });
}

function userExistsCallback(data, exists) {
    if (exists) {
        alert('user ' + data.google.id + ' exists!');
    } else {
        ref.child('users').child(data.uid).set({ //create a new user at the user/<$uid>/ path
            google_id: data.google.id,
            provider: data.provider,
            name: data.google.displayName,
            email: data.google.email,
            pic: data.google.profileImageURL
        });
    }
}
}]);

朋友按Ctrl

app.controller('FriendsCtrl', ['$scope', 'ListFriends', 'Login', function($scope, ListFriends, Login) {
$scope.friends = {
    findNew: {
        query: '',
        search: null,
        found: {},
        add: null
    },
    findCurrent: null
};
$scope.authData = {};
Login.auth().then(function successCallback(data) {
    $scope.authData.uid = data.uid;
}, function errorCallback(data) {
    console.log(data);
});
$scope.friends.findCurrent = function() {
    ListFriends.fetch($scope.authData.uid).then(function successCallback(data) {
        /* Format for the data comes packaged like this:
    {
    <uid>: {
        email: <email>,
        google_ID: <googleID>,
        name: <name>,
        pic: <picURL>,
        uid: <uid>
        }
    } 

    Store the dynamic uids in an array that can be ennumerated through.
    We are going to store this in a $scope.friends object        
    */
        var ojectIndicies = Object.keys(data);
        for (var i = 0; i < objectIndicies.length; i++) {
            var id = data[objectIndicies[i]].uid;
            var name = data[objectIndicies[i]].name;
            var email = data[objectIndicies[i]].email;
            var pic = data[objectIndicies[i]].pic;
            $scope.friends = {
                id: {
                    name: name,
                    email: email,
                    pic,
                    pic
                }
            };
            console.dir("The Friends object now looks like this:" + $scope.friends)
        }
    }, function errorCallback(data) {
        console.log(data);
    });
}
}]);

任何帮助都会很棒
编辑#1: 我已将整个功能放在plnkr中 here。到目前为止,没有任何建议产生影响,我想知道这是否可能是一个HTML问题,所以这就是我创建plnkr的原因。

由于

0 个答案:

没有答案