SFAH
SFAH

Reputation: 644

AngularJS : How to send the data to service

In the following code I successfully get the rating in $scope.hero.rating and order in $scope.hero.order now I want to pass these two to the service as a Hero object to the OrderService.ratingHero which is defined in OrderService class , When I am doing this like

OrderService.ratingHero($scope.hero).then(function(response)

It gives me an error 500... Anybody help me how to solve this?

'use strict';
angular.module('Orders').controller('RatingHeroCtrl',['$scope','$state', '$ionicModal', 'MessageService', 'SettingService', 'OrderService','UserService',

  function($scope, $state, $ionicModal, MessageService, SettingService,OrderService,UserService) {
    $scope.heroName = "Danish";
    (function initialize(){
        $scope.hero = {};
    $scope.rider = {
      ratingsObject : {
                        iconOn: 'ion-ios-star', //Optional
                        iconOff: 'ion-ios-star-outline', //Optional
                        iconOnColor: 'rgb(200, 200, 100)', //Optional
                        iconOffColor: 'rgb(200, 100, 100)', //Optional
                        rating: 0, //Optional
                        minRating: 0, //Optional
                        // readOnly: ratingReadOnly, //Optional
                        callback: function(rating, index) { //Mandatory    

                            $scope.ratingsCallback(rating,index);
                        }
                    }
        }
    })()
$scope.ratingsCallback = function(rating, index) {

  $scope.hero.rating = rating;

OrderService.getOrder($state.params.orderId,
            function(response) {
                $scope.hero.order = response;
            console.log("order"+$scope.hero.order);
            },
            function(error){
                console.log("error");


            }
            )
    }]);

OrderService.js

angular.module('Orders')
    .service('OrderService', ['$http', '$state', '$resource', '$q', 'SettingService', '$localStorage', "MessageService",
     function($http, $state, $resource, $q, SettingService, $localStorage, MessageService) {
        var orderResource = $resource(SettingService.baseUrl + "api/orders/:id", {id:'@id'}, {'query':{method:'GET', isArray:false}, 'update':{method:'PATCH'}});
        var service = {
 ratingHero : function(hero){
                return $http({
                    method: "POST",
                    url: SettingService.baseUrl + "api/heroRatings",
                    data: hero
                });
            },

getOrder : function(OrderId, successCallback, failureCallback){
                orderResource.query({id:OrderId}, successCallback, failureCallback);
            }
        }
        return service;
    }]);

Upvotes: 2

Views: 143

Answers (2)

georgeawg
georgeawg

Reputation: 48968

Use $q.all to wait for the two items before calling the subsequent service:

var ratingPromise = service.getRating();
var orderPromise = service.getOrder();

//USE $q.all
$q.all([ratingPromise, orderPromise]).then(function()  {
     //CHAIN POST
     return service.postHero();
}).then(function(response) {
     console.log("Success");
}).catch(function(error) {
     console.log("Error");
     throw error;
});

This way the POST service executes after the data has been returned from the previous queries to the server.

Upvotes: 0

Mistalis
Mistalis

Reputation: 18309

Error 500 means you have an error on your server side:

500 Internal Server Error
A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.

As it is a very generic message, there may be several possible issues, but this problem does not come from your AngularJS code.

I would suggest you to check if you correcly spelled your server name / API in Angular, and to look at your server logs, seeking for an exception/crash/error.

Upvotes: 1

Related Questions