0

What I am trying to achieve is a dropdown list that produces lisof Metacategories. Once the user selects the meta category, the parameter should be passed onto the URI of the function present in $scope.meta().then(...). however, the parameter I am passing is category; ng-model="category". This parameter is not sent to the URI, I am getting an error:

TypeError: Cannot read property 'category' of undefined

HTML:

 </select></center >
<center> <select ng-model="category"  > <!-- This produces drop down list from which the users selects a category which should then get passed to the meta1() function as a parameter to produce a further list for the drop down after that-->
<option size=50 value="" selected>Select a meta category</option>
 <option ng-repeat="meta in metas.metas" value="{{meta}}">{{meta}} </option>

</select></center>
<center> <select ng-model="category"  >
<option size=50 value="" disabled selected>Select a category</option>
 <option ng-repeat="category in categories.categories" value="{{category}}">{{category}} </option>

 </select></center>

Angular:

myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/post', {
    templateUrl: 'templates/post.html',
    controller: 'PostCtrl'
})
}])
.controller('UserListCtrl', function ($scope, $http) {
$scope.list = function () { // this gets the list of places which works completely fine.
    $http.get('http://94.125.132.253:8001/getuncategorisedplaces').success(function (data) {

        $scope.places = data;
        console.log(data);
        $scope.message = 'List of Uncategorised places';
        $scope.meta = function () { // this gets list of meta categories, which is working fine.
            return $http.get('http://94.125.132.253:8000/getmetas').success(function (data) {

                $scope.metas = data;
                console.log(data);
                $scope.message = 'List of Uncategorised places';
            })
        }

// the function below should get a parameter category from previous function which is selected in HTML. Once this happens, the parameter category should be passed on as formdata.category(below) which will get the list of categories.

        $scope.meta().then(function (data) {
            var formdata = {
                'category': this.category,
            }
            var inserturl = 'http://94.125.132.253:8000/getcategories?meta=' + formdata.category;
            $http.get(inserturl).success(function (data) {

                $scope.categories = data;
                console.log(formdata.category);
                console.log(data);
                $scope.message = 'List of Uncategorised places';
            });


        });
    })
}
$scope.list();
4
  • Here is a tip: Format the codes to have a proper indentation so that it is readable and attract people eyes to come and help! Commented Jul 22, 2014 at 10:25
  • Your $scope.meta function doesn't look like it's returning anything, but you're trying to call .then on the result of calling it. Commented Jul 22, 2014 at 10:25
  • oh how do I fix it. I have not used .then before but I want meta1() to be called after meta is called and one of the options has been selected to pass the parameter Commented Jul 22, 2014 at 10:30
  • I would suggest you to create a workable plunkr or jsfiddle to ease others to solve your problem Commented Jul 22, 2014 at 11:38

2 Answers 2

1

I would think that the error is caused by this:

$scope.meta().then(function meta1($scope,$http) {

because the meta method is not returning a promise. The meta method could return a promise if you did this:

$scope.meta= function(){
  return $http.get('http://94.125.132.253:8000/getmetas').success(function (data) {
    $scope.metas = data;
    console.log(data);
    $scope.message = 'List of Uncategorised places';
    return data;
  });
};

and you could change the call to meta to this

$scope.meta().then(function(data) {
...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Missing return.

$scope.meta should return a promise for you to be able to use .then()

$scope.meta= function(){
    return $http.get('http://94.125.132.253:8000/getmetas').success(function(...
        ...
}

$scope.meta().then(function (data) {
    // do something with data
});

16 Comments

This has stopped the error however, the code still does not work. It does not display the data that should come from second URI getcategories
Take a look at the edit. You should make the second call inside the callback.
yes that did help, however, now the problem has occured that it does not recognise the parameter which has been passed from the previous function which is meta.
I have all the original code in here, I have not used jsfiddle before but I have put the code together if that helps
Update your question to make it more clear and explain what you're trying to achieve. Currently it doesn't make much sense.
|

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.