0

Trying to return JSON data is causing a error in my service. Here is the code...looks simple enough!

var app = angular.module('easyjet', []);

app.service('FlightsService', function($http) {

    return {
        'selectedResult': null,
        'resultsData': $http.get('http://ejtestbed.herokuapp.com/flights')
    };

});

app.controller('ResultsController', function($scope, FlightsService) {

    // Default sort setting
    $scope.order = "flightNumber.number";

    // Using service
    $scope.flights = FlightsService;
});

app.controller('DetailedFlightController', function($scope, FlightsService) {

    // Using service
    $scope.flights = FlightsService;

});

How can I fix this? Thanks in advance

7
  • What's the error?? Commented Aug 4, 2016 at 16:04
  • docs.angularjs.org/error/filter/… Commented Aug 4, 2016 at 16:09
  • the second error is : Error: filter:notarray Not an array Commented Aug 4, 2016 at 16:10
  • @Rob The problem is the array its looking for is not being loaded in 'resultsData' in my code Commented Aug 4, 2016 at 16:11
  • 1
    Http requests are asynchronous actions, meaning that they will finish at some point in time after beeing called, succesfully or not. Because data can be returned at some point in time $http.get will return a $promise. Basically saying i will return something at some point. You have 2 callback possibilities 1.success 2.error. $promise.then(function(data){//success;},function(){//err;}). As for the usage @Rob provided a really good fiddle for your case, namely FlightsService.resultsData().then(function(response) {//do smth wih your data}); Commented Aug 4, 2016 at 16:51

1 Answer 1

1

Your resultsData property needs to return a function, not an $http request:

Working example: https://jsfiddle.net/yh26oey6/

 return {
  'selectedResult': null,
  'resultsData': function() {
    return $http.get('https://api.zippopotam.us/us/90210');
  }

And, without seeing how you're calling the service, there could be other issues also.

Sign up to request clarification or add additional context in comments.

1 Comment

Your controller is not calling the resultsData function, it's just referencing the service. Take a look at the fiddle link in my answer to get a better idea of how this could work.

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.