iLiveInAPineappleUnderTheSea iLiveInAPineappleUnderTheSea - 2 months ago 22
AngularJS Question

factory is undefined when using ng-file-upload

I have a form in which I save some details and upload a file. I have a factory from which I get some data. When I use

ng-file-upload
the factory is undefined. Here is the code:

angular.module('tollApp')
.controller('mstrEmployeeCtrl',['Upload','$window',
function(Upload,$window,$scope,$http,$timeout,$filter,$mdToast,userDetailsFactory){

$scope.ipForHttp = userDetailsFactory.getUserDetailsFromFactory().ipAddress;
//`userDetailsFactory` is undefined
$scope.SaveData = function(){
// $scope.dobObj = new Date($scope.Emp.EmpDoB);
if ($scope.myform.$valid && $scope.passwordEqual) {
Upload.upload({
url: '$scope.ipForHttp+"addEmployee?EmpID="+$scope.Emp.EmpID,

data:{file:file} //pass file as data, should be user ng-model
})
.then(function(response){
$scope.error=response.data.code;
console.log(JSON.stringify(response));
console.log($scope.error+" SCOPE");

})
$scope.submitted = false;
}
};

}
]);


The error:

angular.min.js:117 TypeError: Cannot read property 'getUserDetailsFromFactory' of undefined
at new <anonymous> (http://192.168.1.19/public/javascripts/mstrEmployeeCtrl.js:4:38)
at Object.instantiate (http://192.168.1.19/public/javascripts/angular.min.js:41:477)
at http://192.168.1.19/public/javascripts/angular.min.js:90:3
at Object.link (http://192.168.1.19/node_modules/angular-route/angular-route.min.js:7:274)
at http://192.168.1.19/public/javascripts/angular.min.js:16:230
at ia (http://192.168.1.19/public/javascripts/angular.min.js:81:35)
at n (http://192.168.1.19/public/javascripts/angular.min.js:66:176)
at g (http://192.168.1.19/public/javascripts/angular.min.js:58:429)
at http://192.168.1.19/public/javascripts/angular.min.js:58:67
at http://192.168.1.19/public/javascripts/angular.min.js:62:430 <div data-ng-view="" class="ng-scope" data-ng-animate="1">


The factory which is undefined:

angular.module('tollApp')
.controller('indexController', function($scope,$http,$window,userDetailsFactory){
$scope.usernameFromServer={};
$scope.getUserDetails = function(){
$http({
method:'GET',
url:'http://192.168.1.19:80/getUserDetails'
})
.then(function(response){

// console.log(JSON.stringify(response));
userDetailsFactory.setUserDetailsInFactory(response.data);
$scope.usernameFromFactory = userDetailsFactory.getUserDetailsFromFactory().usernameFromSession;

// $scope.usernameFromServer = userDetailsFactory.getUserDetailsFromFactory().username;
// console.log(JSON.stringify($scope.usernameFromFactory)+"usernameFromFactory");
})
}
$scope.logout = function(request,response){
$http({
method:'GET',
url:'/logout'
})
.then(function(response){
console.log(JSON.stringify(response));
if(response.data=="logout"){
$window.location.href="http://192.168.1.19:80/login";
}
})
}
console.log("indexController");
}).factory('userDetailsFactory',function(){
var user = {};
return {
setUserDetailsInFactory : function(val){
user.useridFromSession = val[0].UserID;
user.usernameFromSession = val[0].UserName;
user.userroleFromSession = val[0].UserRole;
user.clientidFromSession = val[0].ClientID;
user.ipAddress = "http://192.168.1.19:80/";
// user.ipAddress = "http://easypaytoll.com/";
// console.log("in set "+user.clientidFromSession);
},
getUserDetailsFromFactory : function(){
return user;
}
};
});

Answer

It's an injection problem. You have to put the userDetailsFactory in the array :

angular.module('tollApp')
     .controller('mstrEmployeeCtrl',['Upload','$window','$scope', '$http','$timeout','$filter','$mdToast','userDetailsFactory'
       function(Upload,$window,$scope,$http,$timeout,$filter,$mdToast,userDetailsFactory){

Edit 1 Your error is clear : userDetailsFactory is not defined. How did you define this factory ? Is it in the same module ? If not, did you add its module to the app's module dependencies ?