AngularJS - Firebase - 承诺错误

AngularJS - Firebase - Promise Error

我正在尝试按照 thinkster.io 上的基本教程进行操作,并且正在解决问题。 https://thinkster.io/learn-to-build-realtime-webapps/ 本教程使用已折旧的 SimpleLogin,因此我相应地更改了代码,但是我不断收到如下错误。

TypeError: 无法读取未定义的 属性 'finally' 在范围内。$scope.register (http://localhost:9000/scripts/controllers/auth.js:9:31)

我认为问题出在我的控制器逻辑中的承诺上。它一直返回未定义?任何帮助,将不胜感激。我可以根据人们的想法添加更多信息。谢谢

服务逻辑

'use strict';

app.factory('Auth', function ($firebaseAuth, $rootScope) {
  var ref = new Firebase('https://park-reservation.firebaseio.com/');
  //var auth = $firebaseSimpleLogin(ref);

  var Auth = {
    register: function (user) {
      return ref.createUser({
        email: user.email,
        password: user.password
      }, function(error, userData) {
        if (error) {
          switch (error.code) {
            case 'EMAIL_TAKEN':
              console.log('The new user account cannot be created because the email is already in use.');
              break;
            case 'INVALID_EMAIL':
              console.log('The specified email is not a valid email.');
              break;
            default:
              console.log('Error creating user:', error);
          }
        } else {
          console.log('Successfully created user account with uid:', userData.uid);
        }
      });
    },
    login: function (user) {
      return ref.authWithPassword({
        email: user.email,
        password: user.password
      }, function(error, authData) {
        if (error) {
          console.log('Login Failed!', error);
        } else {
          console.log('Authenticated successfully with payload:', authData);
        }
      });
    },
    logout: function () {
      ref.unauth();
    },
    resolveUser: function() {
      return ref.getAuth();
    },
    signedIn: function() {
      return !!Auth.user.provider;
    },
    user: {}
  };

  $rootScope.$on('login', function(e, user) {
    console.log('logged in');
    angular.copy(user, Auth.user);
  });
  $rootScope.$on('logout', function() {
    console.log('logged out');
    angular.copy({}, Auth.user);
  });

  return Auth;
});

控制器逻辑

app.controller('AuthCtrl', function ($scope, $location, Auth, user) {
  if (user) {
  //  $location.path('/');
  }

  $scope.register = function () {
    Auth.register($scope.user).finally(function() {
      return Auth.login($scope.user).finally(function() {
        $location.path('/');
      });
    });
  };
});
Firebase 的

createUserauthWithPassword 方法不会 return 任何东西,因此在未定义的值上调用 finally 方法将引发错误。

您应该向每个 registerlogin 方法添加一个新参数(回调函数),一旦 Firebase 方法响应,这些方法就会被调用。

服务变更

  register: function (user, cb) {
      return ref.createUser({
        email: user.email,
        password: user.password
      }, function(error, userData) {
        if (error) {
          switch (error.code) {
            case 'EMAIL_TAKEN':
              cb('The new user account cannot be created because the email is already in use.');
              break;
            case 'INVALID_EMAIL':
              cb('The specified email is not a valid email.');
              break;
            default:
              cb('Error creating user:', error);
          }
        } else {
          //Successfully created user account
          cb(null, userData.uid);
        }
      });
    },
    login: function (user, cb) {
      return ref.authWithPassword({
        email: user.email,
        password: user.password
      }, function(error, authData) {
        if (error) {
          //Login failed
          cb(error);
        } else {
          cb(null, authData);
        }
      });
    },

控制器变化

  $scope.register = function () {
    Auth.register($scope.user, function(err, userId) {
      if (err) {
        //show error 
        return;
      }
      //You can add the userId returned by register method to user object
      $scope.user.Id = userId;
      Auth.login($scope.user, function(err, authData) {
        if (err) {
          //show error 
          return;
        }
        $location.path('/');
      });
    });
  };