TypeError:无法读取undefined ionic-v1的属性'uid'

时间:2017-05-08 04:10:41

标签: facebook firebase ionic-framework firebase-realtime-database firebase-authentication

关于如何使用属性uid通过facebook / firebase进行身份验证来解决此问题,我有以下问题。这将根据图像在命令提示符中显示错误消息。 Print Error Prompt

服务

  'use strict';


app.factory('Auth', function ($firebaseAuth, $firebaseObject, $firebaseArray, $state, $http, $q, $cordovaOauth) {
var ref = firebase.database().ref();
var auth = $firebaseAuth();

var Auth = {

    createProfile: function(uid, profile) {
        return ref.child('profiles').child(uid).set(profile);
    },

    getProfile: function(uid) {
        return $firebaseObject(ref.child('profiles').child(uid));
    },

    login: function() {
        var provider = new firebase.auth.FacebookAuthProvider();
        provider.addScope('public_profile, email, user_location, user_birthday, user_photos, user_about_me');



      //  return auth.$signInWithPopup(provider)
      //      .then(function(result) {
                  $cordovaOauth.facebook("1455510817798096", ["public_profile", "email", "user_location", "user_birthday", "user_photos", "user_about_me"])
                  .then(function (result) {


                //var accessToken = result.credential.accessToken;
                var user = Auth.getProfile(result.user.uid).$loaded();
                //ver ideia do tairone pra ver o que acontece
               //  var user = Auth.getProfile(result.user).$loaded();


                user.then(function(profile) {
                    if (profile.name == undefined) {

                        var genderPromise = $http.get('https://graph.facebook.com/me?fields=gender&access_token=' + accessToken);
                        var birthdayPromise = $http.get('https://graph.facebook.com/me?fields=birthday&access_token=' + accessToken);
                        var locationPromise = $http.get('https://graph.facebook.com/me?fields=location&access_token=' + accessToken);
                        var bioPromise = $http.get('https://graph.facebook.com/me?fields=about&access_token=' + accessToken);
                        var imagesPromise = $http.get('https://graph.facebook.com/me/photos/uploaded?fields=source&access_token=' + accessToken);
                        var promises = [genderPromise, birthdayPromise, locationPromise, bioPromise, imagesPromise];

                        $q.all(promises).then(function(data) {
                            var info = result.user.providerData[0];
                            var profile = {
                                name: info.displayName,
                                email: info.email,
                                avatar: info.photoURL,
                                gender: data[0].data.gender ? data[0].data.gender : "",
                                birthday: data[1].data.birthday ? data[1].data.birthday : "",
                                age: data[1].data.birthday ? Auth.getAge(data[1].data.birthday) : "",
                                location: data[2].data.location ?  data[2].data.location.name : "",
                                bio: data[3].data.about ? data[3].data.about : "",
                                images: data[4].data.data
                            }
                            Auth.createProfile(result.user.uid, profile);
                            //mudar a profile tambem mudando ideia tairone
                              //Auth.createProfile(result.user, profile);

                        });
                    }
                });
            });
    },

    logout: function() {
        return auth.$signOut();
    },
     getAbout: function(access_token) {
        return $http.get('https://graph.facebook.com/me?fields=bio&access_token=' + access_token);
    },



    getAge: function(birthday) {
        return new Date().getFullYear() - new Date(birthday).getFullYear();
    },

    requireAuth: function() {
        return auth.$requireSignIn();
    },
    getProfilesByAge: function(age) {
        return $firebaseArray(ref.child('profiles').orderByChild('age').startAt(18).endAt(age));
    },
    getProfiles: function(){
        return $firebaseArray(ref.child('profiles'));
    }               

};



  auth.$onAuthStateChanged(function(authData) {
    if(authData) {
        console.log('Este usuario ja se encontra logado!');
    } else {
        $state.go('login');
        console.log('Este usuario ainda nao se encontra logado no aplicativo da cleo.');
    }
});

return Auth;

});

控制器

'use strict';



app.controller('AuthCtrl', function(Auth, $state) {

var auth = this;

auth.login = function() {
    console.log('Usuario clicou no botao para realizar login!');

    //  return Auth.login().then(function(user) {
         return Auth.login(function(user) {
        $state.go('app.home');
    });

};



auth.logout = function() {
    Auth.logout();
};
});

0 个答案:

没有答案