0

It's been a while since I've used $resource for managing my service calls.

For some reason, all my calls are working ok and reaching my REST end-points, basically /api/profile and /api/profile/:id.

But for some reason, my put returns as 404.

Anyone have an Idea of what may be going on.

Thanks and Cheers!

'use strict';

angular.module('chainLinkApp')

.config(['$stateProvider', function($stateProvider){
  $stateProvider
  .state('profile', {
    url:'/profile/:id',
    templateUrl:'views/profile.html',
    controller:'ProfileController',
    controllerAs:'profile'
  });
}])

.controller('ProfileController',['$scope', '$http', 'profileFactory', function($scope, $http, profileFactory){


  $scope.updateMode = false;


  $scope.comments = profileFactory.getProfiles.go().$promise.then(function(response){
    $scope.comments = response;
  });


  $scope.getProfile = function(commentId){
    $scope.comment = profileFactory.getProfile.go({id:commentId}).$promise.then(function(response){
      $scope.comment = response;
      $scope.updateMode = true;
    }, function(error){
      return console.log('An error has occured', error);
    });
  };


  $scope.addProfile = function(comment){
    profileFactory.postProfile.go(comment).$promise.then(function(){
      console.log('Your post was a success');
      $scope.comment = {};
    }, function(error){
      console.log('There was an error: ', error);
    });
  };


  $scope.updateProfile = function(comment){
    profileFactory.updateProfile.go(comment._id, comment).$promise.then(function(response){
      console.log('Your profile has been updated');
      $scope.updateMode = false;
      $scope.comment = {};
    }, function(error){
      console.log('There is an error: ', error);
    });
  };
}])


.factory('profileFactory', ['$resource', function($resource){

  return{
    getProfiles:    $resource('/api/profile', {}, { go: { method:'GET', isArray: true }}),
    getProfile:     $resource('/api/profile/:id',{},{ go: { method: 'GET', params: { id: '@id' }}}),
    postProfile:    $resource('/api/profile', {}, { go: { method: 'POST' }}),
    updateProfile:  $resource('/api/profile/:id', {}, { go: { method: 'PUT', params: { id:'@id' }}})
  };

}]);
cnak2
  • 1,711
  • 3
  • 28
  • 53

1 Answers1

0

The way of you are using $resource is strange, it should be like this:

.factory('UserService', function($resource) {
    return $resource('/api/users/:id', {}, {
        'create': { method: 'POST' },
        'update': { method: 'PUT', params: { id: '@id'} }

    });
})

Then you call the service like this: UserService.create(theUserobj, function(result) { ... })

Mavlarn
  • 3,807
  • 2
  • 37
  • 57
  • Thanks Mavlarn. You are right. Been a while since I used it and I was trying to chain a variety of calls, including some custom calls. – cnak2 Aug 05 '16 at 03:09
  • OK, anyway, it seems that there is no problem in your code. Did you check the url in your update request? Which should match the code in server side. Please check the 'id' in you url. – Mavlarn Aug 08 '16 at 07:04