类型错误:未定义是不是与火力地堡和AngularJS(ThinksterIO教程第7章)函数 [英] TypeError: undefined is not a function with Firebase and AngularJS (ThinksterIO Tutorial Chapter 7)
问题描述
更新
本教程已经更新,现在的问题是过时
我碰到与Thinkster.io AngularJS教程多个问题:学习如何构建现代web应用的第7章使用火力创建自己的用户数据。
第一个曾与节能的新用户到火力地堡锻造这令人欣慰的是由安娜闷杀她SO发布<一个解决办href=\"http://stackoverflow.com/questions/25633141/angularjs-tutorial-thinkster-io-chapter-7\">AngularJS教程Thinkster.io第7章但现在我注册一个新用户后,获得两个TypeErrors。我会解释,当我注册一个新用户会发生什么:
- 我填写登记表
- 我点击注册
- 我看到我的新的用户对象登录到控制台
- 我得到登录
- 2
类型错误:未定义不是一个函数
错误出现在控制台
第1类型错误:未定义不是一个函数
所以在看的第一个错误,我们看到被指出,在3个地方我 user.js的
类型错误:未定义不是一个函数
在Object.User.findByUsername(HTTP://本地主机:9000 /脚本/服务/ user.js的:37:18)
在setCurrentUser(HTTP://本地主机:9000 /脚本/服务/ user.js的:8:33)
在http://本地主机:9000 /脚本/服务/ user.js的:32:5
在我的code第一名的第一个错误指的是第37行,这是在 findByUsername`功能$子的开始列18
findByUsername:功能(用户名){
如果(用户名){
回报用户$子(用户名)。
}
},
从那里,它指向线8条,这是 findByUsername
方法调用的开始列33
函数setCurrentUser(用户名){
$ rootScope.currentUser = User.findByUsername(用户名);
}
和最后它指向32行,这是 setCurrentUser
方法调用的开头第5列
用户。$保存()。然后(函数(){
setCurrentUser(用户名);
});
第二类型错误:未定义不是一个函数
在 user.js的
类型错误:未定义不是一个函数
在http://本地主机:9000 /脚本/服务/ user.js的:14:9
这是第14行,第9列在 $关于
方法调用的开头
查询。在$('装',函数(){
(查询$ getIndex()[0])setCurrentUser;
});
为什么我得到这两个TypeErrors?任何想法
文件
user.js的服务
使用严格的;app.factory(用户,函数($火力点,FIREBASE_URL,$ rootScope){
VAR REF =新的火力地堡(FIREBASE_URL +'用户');
VAR用户= $火力点(REF)$ asObject()。 功能setCurrentUser(用户名){
$ rootScope.currentUser = User.findByUsername(用户名);
} $ rootScope在$('$ firebaseSimpleLogin:登录',函数(即AUTHUSER){
VAR的查询= $火力(ref.startAt(authUser.uid).endAt(authUser.uid)); 查询。在('装'$,函数(){
(查询$ getIndex()[0])setCurrentUser;
});
}); $ rootScope在$('$ firebaseSimpleLogin:注销',函数(){
删除$ rootScope.currentUser;
}); VAR USER = {
创建:功能(AUTHUSER,用户名){
用户[用户名] = {
/ * jshint驼峰:假* /
MD5_HASH:authUser.md5_hash,
用户名:用户名,
$优先级:authUser.uid
};
用户。$保存()。然后(函数(){
setCurrentUser(用户名);
});
},
findByUsername:功能(用户名){
如果(用户名){
回报用户$子(用户名)。
}
},
getCurrent:功能(){
返回$ rootScope.currentUser;
},
signedIn:功能(){
!$回报== rootScope.currentUser不确定的;
}
}; 返回用户;
});
auth.js 控制器
使用严格的;app.controller('AuthCtrl',
功能($范围,$位置,验证,用户){
如果(Auth.signedIn()){
$ location.path('/');
} $ $范围在('firebaseSimpleLogin:登录',函数(){
$ location.path('/');
}); $ scope.login =功能(){
Auth.login($ scope.user)。然后(函数(){
$ location.path('/');
},功能(错误){
$ scope.error = error.toString();
});
}; $ scope.register =功能(){
Auth.register($ scope.user)。然后(功能(AUTHUSER){
User.create(AUTHUSER,$ scope.user.username);
$ location.path('/');
Auth.login($ scope.user);
的console.log(AUTHUSER);
},功能(错误){
$ scope.error = error.toString();
});
};
});
检查我的解决方案在编辑1:我有更新code,这应该带走,当你注册你的错误
由于angularfire的0.8版本,他们已经采取了$子()和$的(),我会更新我的编辑2,今晚(当我回家)。
解决方案<一个href=\"http://stackoverflow.com/questions/25633141/angularjs-tutorial-thinkster-io-chapter-7\">AngularJS教程Thinkster.io第7章
也检查了这一点:
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:
- I fill out the registration form
- I click Register
- I see my new user object logged to the console
- I get logged in
- 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 the
findByUsername` 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:
这篇关于类型错误:未定义是不是与火力地堡和AngularJS(ThinksterIO教程第7章)函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!