教程已更新,问题现已过时
我遇到了Thinkster.io AngularJS教程的多个问题:学习构建现代Web应用程序Chapter 7. Creating your own user data using firebase。
首先要将新用户保存到Firebase Forge,谢天谢地,Anna Smother在她的SO帖子AngularJS tutorial Thinkster.io chapter 7中解决了这个问题,但现在我在注册新用户后收到两个TypeErrors。我将解释当我注册新用户时会发生什么:
TypeError: undefined is not a function
个错误
所以看看第一个错误,我们在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`函数的开头
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
是$on
方法调用
query.$on('loaded', function () {
setCurrentUser(query.$getIndex()[0]);
});
关于我为什么会收到这两个TypeErrors的任何想法?
user.js 服务
'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();
});
};
});
答案 0 :(得分:0)
在编辑1中检查我的解决方案:我已经更新了代码,这会在你注册时带走你的错误吗?
自angularfire 0.8版以来,他们已经取出了$ child()和$ on(),我将在今晚更新我的解决方案2(当我回到家中时)。
AngularJS tutorial Thinkster.io chapter 7
同时检查一下: