StrugglingCoder StrugglingCoder - 5 months ago 43
AngularJS Question

Cannot read property success of undefined in Angular JS

In a controller A , I have a method that on click of a submit button stores the data at a MVC controller action method.

$scope.groupmembership.submit = function() {
if ($scope.groupMembershipUserInputForm.$valid) {
$scope.groupmembershipUploadParams = {
"chapterCode": $scope.groupmembership.chapterCode,
"createdDate": $filter('date')(new Date($scope.groupmembership.createdDate), 'MM/dd/yyyy'),
"endDate": $filter('date')(new Date($scope.groupmembership.endDate), 'MM/dd/yyyy')
};

UploadDataServices.getGroupMembershipUploadParams(
$scope.groupmembershipUploadParams)
.success(function (result) {
$rootScope.$emit(
'validateUploadedFilesEvent',
$scope.groupmembershipUploadParams
);
});
}
}


UploadDataServices.getGroupMembershipUploadParams

getGroupMembershipUploadParams: function(uploadParams){
$http.post(BasePath + "uploadNative/setGroupMembershipUploadParams", uploadParams, {
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}).success(function (result) {
console.log(result);
})
},


uploadNativeMVC controller

public async Task<JsonResult> setGroupMembershipUploadParams(GroupMembershipUploadParams arg)
{
TempData["GroupMembershipUserUplaodDetails"] = arg; //Save the data for later use
var result = new JsonResult();
result.Data = "Success"; // Return a Excel Upload Limit Exceeded
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
}


This saved data I want to re-use by other methods in the MVC controller in subsequent calls.

I do not want any response back but just make sure methodB() in angular executes after the data in MVC is saved , I did fired an event after I get success response

UploadDataServices.getGroupMembershipUploadParams(
$scope.groupmembershipUploadParams)
.success(function (result) {
$rootScope.$emit('methodB', $scope.groupmembershipUploadParams);
});


I know this result.data in MVC creation is redundant ,

var result = new JsonResult();
result.Data = "Success"; // Return a Excel Upload Limit Exceeded
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;


but how to make sure

$rootScope.$emit('methodB', $scope.groupmembershipUploadParams);


fires only after tempData is stored.

UploadDataServices.getGroupMembershipUploadParams(
$scope.groupmembershipUploadParams)
.success(function (result)


is throwing me error . What can be done more neatly ? I know there are lot of redundant code lines.

Thanks in advance.

Answer

If you don't have a return in your method, it's going to return undefined. That's why you're getting an error.

Fixed:

getGroupMembershipUploadParams: function(uploadParams){
    return $http.post(
        BasePath + "uploadNative/setGroupMembershipUploadParams",
        uploadParams, 
        {
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json"
           }
        })
        .success(function (result) {
            console.log(result);
        });
},
Comments