I have been working on a beginner project that is essentially a simple task manager (similar to those todo list projects). I created a user login page for this project and here is how it functions.
There are two functions, siteLogin()
for logging in, and inside it, I want to use the second function showTasks()
after the user logs in. This function returns the user tasks obtained from an API using promises.
Initially, I encountered an issue when trying to return a value from an $http call within the showTasks()
function. Instead of getting the desired value, it returned something like $$state
. After researching solutions, I learned that $http doesn't return values but rather returns promises. Despite several attempts and failures, my code now runs up until the showTasks()
function where it halts.
Here is a snippet of my current code:
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");
});
}
}
});
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);
});
}
}]);
Although this problem may seem repetitive as there are similar issues discussed on Stack Overflow, I have tried various solutions without resolving this particular problem and even encountered new ones along the way. I attempted using $q
, nested .then
, defining code in factories, and calling instances in modules, among other approaches, yet the issue persists.
NOTE: Apologies for any language errors in my explanation.