TypeError: undefined 不是 Firebase 和 AngularJS 的函数(ThinksterIO 教程第 7 章) [英] TypeError: undefined is not a function with Firebase and AngularJS (ThinksterIO Tutorial Chapter 7)

查看:18
本文介绍了TypeError: undefined 不是 Firebase 和 AngularJS 的函数(ThinksterIO 教程第 7 章)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

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

<小时>

我在 Thinkster.io AngularJS 教程:学习构建现代 Web 应用程序中遇到了多个问题

还请检查一下:

AngularFire api

Update

The tutorial has been updated and the question is now outdated


I've run into multiple issues with the Thinkster.io AngularJS Tutorial: Learn to build Modern Webapps Chapter 7. Creating your own user data using firebase.

The first had to do with saving a new user to the Firebase Forge which thankfully was solved by Anna Smother in her SO post AngularJS tutorial Thinkster.io chapter 7 but now I am getting two TypeErrors after registering a new user. I will explain what happens when I register a new user:

  1. I fill out the registration form
  2. I click Register
  3. I see my new user object logged to the console
  4. I get logged in
  5. 2 TypeError: undefined is not a function errors appear in the console


1st TypeError: undefined is not a function

So looking at the first error we see 3 places being pointed to in my 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

The first place in my code that the first error points to is line 37, column 18 which is the beginning of $childin thefindByUsername` function

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

From there it points to line 8, column 33 which is the beginning of the findByUsername method call

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

And lastly it points to line 32, column 5 which is the beginning of the setCurrentUser method call

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


2nd TypeError: undefined is not a function

The second TypeError points at only one place in user.js

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

which is line 14, column 9 at the beginning of the $on method call

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

Any ideas on why I'm getting these two 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 controller

'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();
      });
    };
  });

解决方案

Check my solution in Edit 1: I have update the code, this should take away your error when you are registering?

Since version 0.8 of angularfire, they have taken out $child() and $on(), I will update my solution for edit 2, tonight (when I get home).

AngularJS tutorial Thinkster.io chapter 7

Also check this out:

AngularFire api

这篇关于TypeError: undefined 不是 Firebase 和 AngularJS 的函数(ThinksterIO 教程第 7 章)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆