javascript - Cannot read property success of undefined in Angular JS - Stack Overflow

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

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.

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.

Share Improve this question edited Jun 23, 2016 at 15:06 JLRishe 102k19 gold badges137 silver badges171 bronze badges asked Jun 23, 2016 at 12:12 StrugglingCoderStrugglingCoder 5,03116 gold badges72 silver badges110 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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);
        });
},

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744840240a4596514.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信