0

I've a factory method in my Code as:-

    (function () {
    var app = angular.module('MyApp', []);  
    app.controller('MYController', function ($scope, CourseDescFactory) {  // here $scope is used for share data between view and controller
        $scope.MyDescCourses = [];
        CourseDescFactory.OurCourses().then(function (d) {
            $scope.MyDescCourses = d.data;
            $scope.MainHeading = $scope.MyDescCourses[0].CD.MainHeading;
            $scope.MainDesc = $scope.MyDescCourses[0].CD.MainDesc;
            $scope.MainImg = $scope.MyDescCourses[0].CD.Img;

            $scope.MySubDescCourses = [];
            $scope.MySubDescCourses = $scope.MyDescCourses[0].CSD;
            //$scope.Img = $scope.MyDescCourses[0].Img;
        }, function (error) {
            alert('Cannot Load Course Detail');
        });
    })
.factory('CourseDescFactory', function ($http) {
    var CourseDescList = {};
    CourseDescList.OurCourses = function (id) {
        return $http({
            method: 'GET',
            url: '/Home/CourseDesc',
            params: {}
        });
    }
    return CourseDescList;
});

})();

This works fine when I've data from backend . But when there is no data, instead of showing alert('Cannot Load Course Detail'); it throws an error 'Cannot read property 'CD' of undefined'.

Any help will be appreciated.

2
  • 1
    so I'm guessing on your .net HomeController when you have a no data found scenario it is still returning a OK (or 200) can you change your .net controller to throw BadRequest("no data found") when there is no data? Commented Jan 12, 2018 at 2:26
  • Yeah I was not sending any BadRequest. I figured that out later, thanks for your comments. Commented Jan 13, 2018 at 6:23

1 Answer 1

1

It is better to check your response variable which is "data" and it's inside content with the following commands.

     if (
         (d.data != undefined) && (d.data != null) && (d.data != "") 
        )
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I figured out the mistake. Thanks for your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.