0

I take input and look it up in the description of each item. The console statement in the service shows the desired output, but the promise in ctrl is where the error occurred. What am I missing?

    NarrowItDownController.$inject=['MenuSearchService'];

    function NarrowItDownController(MenuSearchService) {
        var ctrl = this;
        ctrl.searchTerm = "";
        ctrl.found=[];
        ctrl.searchlist = function () {
            if (ctrl.searchTerm.length > 0) {
                console.log(ctrl.searchTerm);
                var promise = MenuSearchService.getMatchedMenuItems(ctrl.searchTerm);
                promise.then(function (result) {
                    ctrl.found = result;
                }).catch(function (error) {
                    console.log("something went wrong!!!");
                });
            }
        };
    }

    MenuSearchService.$inject = ['$http'];

    function MenuSearchService($http)  {
        var service= this; 
        var found = [];
        service.getMatchedMenuItems = function (searchTerm) {
            var response = $http({
                method: "GET",
                url: ("https://davids-restaurant.herokuapp.com/menu_items.json")
            }).then(function (response) {
                for (var i = 0; i < response.data.menu_items.length; i++) {
                    if (response.data.menu_items[i]
                            .description.toLowerCase().indexOf(searchTerm)>-1 ) {
                        found.push(response.data.menu_items[i]);
                    }
                }
                console.log(found);
                return found;
            }, function() {
                console.log('error');
            });
        };
    }

}) ();
4444
  • 3,541
  • 10
  • 32
  • 43
Vandy
  • 13
  • 1
  • 3
  • 1
    The indentation is horrible... you've got one more closing brace and parenthesis than opening ones. – trincot Jul 11 '17 at 18:38

1 Answers1

3

You never return the created promise from your function getMatchedMenuItems so calling promise.then(function (result) will fail. Change this line in getMatchedMenuItems from

var response = $http({...

to

return $http({...

The solution is to work with promises throughout the whole stack. Have that service function return the promise and let the controller call then so it can do work once the promise is resolved.


I am tempted to mark this as duplicate of How do I return the response from an asynchronous call?. I recommend reading through it at least which should give you a better idea of how to work with promises.

Igor
  • 60,821
  • 10
  • 100
  • 175
  • 1
    In short, OP forgot to return `$http` promise in `service.getMatchedMenuItems` – Icycool Jul 11 '17 at 18:41
  • @Icycool - I hesitate to use `forgot` in this case as they are returning a variable called `found` so looks like it is intentional and they are not sure how to work with promises. – Igor Jul 11 '17 at 18:43
  • @lcycool, also `MenuSearchService` lacks a return value. – trincot Jul 11 '17 at 18:44
  • I see it now, it looked to me like they were returning something but that's not true it just appeared that way to me due to the horrible indentation. – Igor Jul 11 '17 at 18:45
  • 1
    Just fixed the indentation now... – trincot Jul 11 '17 at 18:45
  • Thank you. I just fixed it and it works. I just started coding so forgive me if you found the code ignorant. i think i have to go back and learn more about promises. Thanks again :) – Vandy Jul 11 '17 at 19:01
  • @VandyVarma - Your code is not bad for just starting out but if you use proper indentation it would be much easier to read (*and spot errors*). Also read the suggested duplicate, it has some great answers on how promises work and how to code with them. Glad you got it working. – Igor Jul 11 '17 at 19:04