TypeError: undefined 不是 Firebase 和 AngularJS 的函数(ThinksterIO 教程第 7 章)

2024-05-07

Update

教程已更新,问题现已过时


我在 Thinkster.io AngularJS 教程:学习构建现代 Web 应用程序中遇到了多个问题第 7 章 使用 firebase 创建您自己的用户数据 http://www.thinkster.io/angularjs/4DYrJRxTyT/7-creating-your-own-user-data-using-firebase.

第一个与将新用户保存到 Firebase Forge 有关,值得庆幸的是 Anna Smother 在她的 SO 帖子中解决了这个问题AngularJS 教程 Thinkster.io 第 7 章 https://stackoverflow.com/questions/25633141/angularjs-tutorial-thinkster-io-chapter-7但现在我在注册新用户后收到两个类型错误。我将解释注册新用户时会发生什么:

  1. 我填写注册表
  2. 我点击注册
  3. 我看到我的新用户对象已记录到控制台
  4. 我登录了
  5. 2 TypeError: undefined is not a function控制台中出现错误

第一个类型错误:未定义不是一个函数

因此,查看第一个错误,我们看到我的文件中指向了 3 个位置user.js

TypeError: undefined is not a function
    at Object.User.findByUsername (http://localhost:9000/scripts/services/user.js:37:18)
    at setCurrentUser (http://localhost:9000/scripts/services/user.js:8:33)
    at http://localhost:9000/scripts/services/user.js:32:5

我的代码中第一个错误指向的第一个位置是第 37 行第 18 列,这是 $child 的开头in the按用户名查找`函数

findByUsername: function (username) {
  if (username) {
    return users.$child(username);
  }
},

从那里它指向第 8 行第 33 列,这是findByUsername方法调用

function setCurrentUser(username) {
  $rootScope.currentUser = User.findByUsername(username);
}

最后它指向第 32 行第 5 列,这是setCurrentUser方法调用

  users.$save().then(function() {
    setCurrentUser(username);
  });

第二个类型错误:未定义不是函数

第二个 TypeError 只指向一个地方user.js

TypeError: undefined is not a function
    at http://localhost:9000/scripts/services/user.js:14:9

也就是开头的第 14 行第 9 列$on方法调用

query.$on('loaded', function () {
  setCurrentUser(query.$getIndex()[0]);
});

关于为什么我收到这两个 TypeErrors 有什么想法吗?


FILES

user.js service

'use strict';

app.factory('User', function ($firebase, FIREBASE_URL, $rootScope) {
  var ref = new Firebase(FIREBASE_URL + 'users');
  var users = $firebase(ref).$asObject();

  function setCurrentUser(username) {
    $rootScope.currentUser = User.findByUsername(username);
  }

  $rootScope.$on('$firebaseSimpleLogin:login', function (e, authUser) {
    var query = $firebase(ref.startAt(authUser.uid).endAt(authUser.uid));

    query.$on('loaded', function () {
      setCurrentUser(query.$getIndex()[0]);
    });
  });

  $rootScope.$on('$firebaseSimpleLogin:logout', function() {
    delete $rootScope.currentUser;
  });

  var User = {
    create: function (authUser, username) {
      users[username] = {
        /*jshint camelcase: false */
        md5_hash: authUser.md5_hash,
        username: username,
        $priority: authUser.uid
      };
      users.$save().then(function() {
        setCurrentUser(username);
      });
    },
    findByUsername: function (username) {
      if (username) {
        return users.$child(username);
      }
    },
    getCurrent: function () {
      return $rootScope.currentUser;
    },
    signedIn: function () {
      return $rootScope.currentUser !== undefined;
    }
  };

  return User;
});

auth.js控制器

'use strict';

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

    $scope.$on('firebaseSimpleLogin:login', function() {
      $location.path('/');
    });

    $scope.login = function() {
      Auth.login($scope.user).then(function() {
        $location.path('/');
      }, function(error) {
        $scope.error = error.toString();
      });
    };

    $scope.register = function () {
      Auth.register($scope.user).then(function (authUser) {
        User.create(authUser, $scope.user.username);
        $location.path('/');
        Auth.login($scope.user);
        console.log(authUser);
      }, function(error) {
        $scope.error = error.toString();
      });
    };
  });

检查编辑 1 中的解决方案:我已经更新了代码,这应该会消除您注册时的错误?

从 angularfire 0.8 版本开始,他们已经删除了 $child() 和 $on(),我将在今晚(当我回家时)更新我的编辑 2 的解决方案。

AngularJS 教程 Thinkster.io 第 7 章 https://stackoverflow.com/questions/25633141/angularjs-tutorial-thinkster-io-chapter-7

另请检查一下:

AngularFire API https://www.firebase.com/docs/web/libraries/angular/api.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TypeError: undefined 不是 Firebase 和 AngularJS 的函数(ThinksterIO 教程第 7 章) 的相关文章

随机推荐