Hello everyone, I just started to learn AngularJS, so do not judge strictly. I have this question: I get this JSON from the API:

{ "isCar": true // false } 

There is a service that knocks on the IPA and takes what has come:

 function checkIfCar(carId) { $http.get(env.api + 'cars/iscar/' + carId).success(response => { return response.data.isCar; //так я хочу достать true или false }).error(function (data) { toastr.error('Something went wrong and we can\'t check:' + data); }); } 

There is a directive from which I call this method:

 function checkIfCar() { var isCar = carService.checkIfCar(carId); if (isCar === false) { toastr.warning('This is not a car!'); } } 

When I try to get it, everything comes good with the IPA, but Angulyar swears

Cannot read property 'isCar' of undefined

Tell me, please, how to extract the value that came, I will be very grateful!

  • one
    Are you aware that $http.get is an asynchronous method and how to work with promises? - MrFylypenko
  • @MrFylypenko, I only know that promises are needed to get an answer and its subsequent transformation. Maybe I don’t understand correctly, but I'm trying to figure it out with all my strength - Dialkord
  • in success remove the return and add the code if (response.data.isCar === false) { toastr.warning('This is not a car!'); } if (response.data.isCar === false) { toastr.warning('This is not a car!'); } - MrFylypenko
  • response.data.isCar , Cannot read property 'isCar' of undefined - better start with a printout of the response in the console and a careful examination of its structure. and only then promises, nuggets, sneakers - nörbörnën
  • @MrFylypenko, did, but the result is the same - Dialkord

0