Search code examples
angularjsangularjs-service

Angular $resource not passing the full URL


SELF EDIT:

I am not sure what the correct protocol here is when I mess up a post... But I messed this one up. The log statement I noted below is actually coming from the other correct execution of the bill detail page. It is correct.

I do not see a log message for the billImages request I am trying to make. I do get some data returned by that service call though, and I'm still looking into why.

Apologies to any kind soul who invested brain matter into thinking about my faulty post.


I am new to AngularJS and struggling to figure this out.

I am using Angular's $resource to call a NodeJS webservice I am also writing.

My service code:

(function(){
    'use strict';

    //BillImages service used for communicating with the billImages REST endpoints
    angular.module('bills').factory('BillImages', billImagesService);

    function billImagesService($resource) {
        return {
            getBillImages: getBillImages
        };

        function getBillImages(){
            return $resource(
                'bills/:billId/billImages', 
                {billId: '@_id'},
                {getBillImages: {method: 'GET'}}
            );
        }
    }
}());

When the server gets the request, it comes in with:

GET /bills/55551963b969c76c241e8e4c 304 47.072 ms - -

My problem is the "/billImages" portion of the URL is being lopped off somewhere between the service call and the server!!! Why?

I am trying to create a page that shows the details for a single bill, but also shows a list of billImages that are children of the bill.

Here is the controller code that is calling the angular service (if it matters),

// Find the latest bill images for the current bill
$scope.findCurrentImages = function() {
    $scope.billImages = BillImages.getBillImages({
        billId: $stateParams.billId
    });
};

I'd appreciate both a solution and advice on improving my approach.


Solution

  • Self answer.

    I'm still not clear on everything here, but switching the controller from:

    $scope.findCurrentImages = function() {
        $scope.billImages = BillImages.getBillImages({
            billId: $stateParams.billId
        });
    };
    

    to:

    $scope.findCurrentImages = function() {
        $scope.billImages = BillImages.query({
            billId: $stateParams.billId
        });
    };
    

    AND reverting by service back to the template provided by the MeanJS sample app:

    angular.module('bills').factory('BillImages', ['$resource',
        function($resource) {
            return $resource(
                'bills/:billId/billImages/:billImageId', 
                {billId: '@_id'}, 
                {update: {method: 'PUT'}}
            );
        }
    ]);
    

    Made the REST service calls start to happen again.