I've been learning/working on a newbie project which is basicly a simple task manager(similiar to those todo list projects). And I designed a user login page for this project and here is how it works.
So I have two functions siteLogin()
for logging in and inside it I want to use second function showTasks()
after user logs in which returns the usertasks it gets from API (promises I know).
So first I needed to return a value from a $http inside the showTasks()
function but I ended up returning something like $$state
so I searched and found couple of solutions on this website and so far I learned that $http doesn't return value but returns promises. So after couple of try&fail my code now runs until showTasks()
and stops there.
So here is my code now.
Factory
app.factory('userTaskList', function ($http) {
return {
showTasks: function (userName) {
var tasks = { K_ADI: userName }; //UserName of the per
var $promise = $http({
method: 'POST',
url: 'http://localhost:5169/api/Isler/' + userName + '/IstenilenKayitCek',
headers: {
'Content-Type': 'application/json'
},
data: tasks
});
$promise.then(function successCallback(response) {
var data = response.data;
console.log("Factory data:", response);
return success(data);
}, function errorCallback(response) {
error("Error");
});
}
}
});
And my Controller:
app.controller('myCtrl', ['$scope', '$http', function ($scope, $http,userTaskList ) {
$scope.siteLogin = function () {
var loginMember = {
K_ADI: $scope.panel.loginUserName,
PAROLA: $scope.panel.loginPassword // HTML input
};
console.log(loginMember);
$http({
method: 'POST',
url: 'http://localhost:5169/api/Kullanicilar/KullaniciDogrula',
headers: {
'Content-Type': 'application/json'
},
data: loginMember
}).then(function successCallback(response) {
console.log("Message sent", response);
$scope.data = response.data.error.data;
if ($scope.data === true) {
console.log("User exists in database");
//RUNS UNTIL HERE AND STOPS
userTaskList.showTasks($scope.panel.loginUserName)
.then(function (res) {
$scope.gorev = res;
console.log("Fonk ici : ", $scope.gorev);
console.log("222222", res);
}, function (err) {
console.log(err);
});
console.log("outside func : ", $scope.gorev);
}
}, function errorCallback(response) {
console.log("Error: ", response);
});
}
}]);
This may be seen like a duplicate and there are many similiar problems on stack but I tried those solutions (I will link some later) still didn't solve my this problem plus some of them created other problems like this one. I tried to use $q
, nested .then
, and finally defining code in factory then calling its instance in module and so on. But still not working.
NOTE:Sorry for my poor English.
response.data.error.data
, could this be producing a null pointer exception? – Abrasive"User exists in database
and I dont see the rest of the consol logs. about theresponse.data.error.data
its because its agroup Project and my friend coded API in a lame way. So I needed to reach the data like that. I used it in other functions that I didnt posted here so I know its not that. – Quincey