[英]AngularJS 'Cannot read property 'then' of undefined'
I've this problem, when I click on login button, the chrome console log this: 我有这个问题,当我点击登录按钮时,chrome控制台会记录下这个:
angular.min.js:117 TypeError: Cannot read property 'then' of undefined at m.$scope.logIn (loginModuleController.js:11)
angular.min.js:117 TypeError:无法在m。$ scope.logIn(loginModuleController.js:11)中读取未定义的属性'then'
Service: 服务:
angular.module('loginModule')
.factory('loginService', function($http){
return{
login: function(username, password){
var session;
$http.post('../server/php/auth/auth.php', {
username: username,
password: password
})
.then(function(res){
session = res;
});
return session;
},
isLogged: function(){
return $http.get('../angCMS/server/php/auth.php?is_logged=true');
},
logOut: function(){
return $http.get('../angCMS/server/php/auth.php?logout=true');
}
};
});
controller: 控制器:
angular.module('loginModule')
.controller('LoginCtrl', ['$scope', 'loginService', function($scope, loginService){
$scope.auth = false;
$scope.username;
$scope.password;
$scope.logIn = function(){
loginService.login($scope.username, $scope.password).then(function(response){
}, function(res){
});
};
$scope.isLogged = function(){
loginService.isLogged()
.then(function(response){
if(response){
$scope.auth = true;
}
});
};
$scope.logOut = function(){
loginService.logOut()
.then(function(response){
if(response){
$scope.auth = false;
}
});
};
}]);
and this is the html template: 这是html模板:
<div class="container" ng-if="auth==false">
<div class="col-md-4 col-md-offset-4">
<div class="row">
<br/><h2 align="center">Login</h2>
</div>
<div class="well">
<form class="form-horizontal">
<fieldset>
<div class="form-group">
<input type="text" class="form-control" placeholder="Username" ng-model="username" required>
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password" ng-model="password" required>
</div>
<div class="form-group">
<button class="btn btn-md btn-primary btn-block" type="submit" ng-click="logIn()">Sign in</button>
</div>
</fieldset>
</div>
</form>
</div>
</div>
PHP login method: PHP登录方法:
public function login($user, $pass){
$user = htmlspecialchars(trim($user));
$pass = md5(htmlspecialchars(trim($pass)));
$res = $this->DB->prepare("SELECT * FROM `admin` WHERE username = :user");
if(!$res->execute(Array(":user"=>$user)))
die(mysql_error());
$row = $res->fetch(PDO::FETCH_ASSOC);
if(!$row['password'] == $pass)
die("Errore: password errata!");
$_SESSION['logged'] = $row;
array_push($session, $_SESSION['logged'], true);
return $session;
}
You're returning an unassigned variable named "session". 您将返回名为“session”的未分配变量。 So what you are doing here in your logged in method is returning undefined.
那么你在登录方法中所做的就是返回undefined。 Undefined does not have a method called then.
Undefined没有一个名为then的方法。
login: function(username, password){
var session;
$http.post('../server/php/auth/auth.php', {
username: username,
password: password
})
.then(function(res){
session = res;
});
return session;
},
My guess is you actually want to return the $http.post method, which in turn returns a promise. 我的猜测是你实际上想要返回$ http.post方法,后者又返回一个promise。 That way you can use the session in the controller?
这样你可以在控制器中使用会话?
Your service function login
is failing to return the promise created by the call to $http.post
. 您的服务功能
login
无法返回由$http.post
调用创建的承诺。 Instead, you need this: 相反,你需要这个:
login: function(username, password){
return $http.post('../server/php/auth/auth.php', {
username: username,
password: password
});
}
Note too that I've removed the session
variable from the function. 另请注意,我已从函数中删除了
session
变量。 It's the then
function in your controller that needs to be dealing with the response. 这是
then
在你的控制器功能需要被处理的响应。
Your isLogged
and logOut
functions look good already. 您的
isLogged
和logOut
函数已经很好看了。 Just repeat that pattern. 请重复这种模式。
This is more of a misuse of promises issue. 这更多是滥用承诺问题。
You might want to first take a look of how promises work: 您可能想先了解一下Promises的工作原理:
From your service code: 从您的服务代码:
login: function(username, password){
var session;
$http.post('../server/php/auth/auth.php', {
username: username,
password: password
})
.then(function(res){
session = res;
});
return session;
}
When login(username, password)
is called, session is still undefined
upon returning. 当调用
login(username, password)
时,返回时会话仍未undefined
。
This is because $http.post()
is an asynchronous function, the .then()
clause does not get executed immediately. 这是因为
$http.post()
是一个异步函数, $http.post()
.then()
子句不会立即执行。
As pointed out by Snixtor's answer , you should "return the return value of $http.post()
": 正如Snixtor的回答所指出的那样,你应该“返回
$http.post()
的返回值”:
login: function(username, password){
return $http.post('../server/php/auth/auth.php', {
username: username,
password: password
});
}
Then referring to your controller's logIn method: 然后参考你的控制器的logIn方法:
$scope.logIn = function(){
loginService.login($scope.username, $scope.password).then(function(response){
// response === 'session'
}, function(res){
});
};
the response
parameter from loginService.login().then()
is exactly the value of your intended session
variable from your previous implementation. 来自
loginService.login().then()
的response
参数正是您之前实现中的预期session
变量的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.