1

This is what I wrote in my factories.js:

app.factory('CustomerCompany', function($resource){
    return $resource('/customer_companies/:id.json', {id: "@_id"}, {
        'query':{method: 'GET', isArray:false},
        'getByCustomerAccount':{
            method: 'GET', 
            params: {customer_account_id: customer_account_id}, 
            isArray:true, 
            url:'/customer_companies/get_by_account/:customer_account_id.json'
        }
    });
});

Because I want to fetch a list of customer_companies that belong to a particular customer_account so I need to supply a customer_account_id.

In my controllers.js,

app.controller('customerAccountEditController', function($scope, CustomerCompany) {
    $scope.data = {};
    var path        = $location.path();
    var pathSplit   = path.split('/');
    $scope.id       = pathSplit[pathSplit.length-1];
    var customer_account_id = $scope.id;

    $scope.list_of_companies = [];

    CustomerCompany.getByCustomerAccount({customer_account_id: customer_account_id}, function(data){
        console.log(data);
        //$scope.list_of_delegates.push(data);
    }); 
});

I get a customer_account_id not defined.

Where did I go wrong?

3 Answers 3

1

I think you don't need to define params inside your $resource & then URL will becomes url:'/customer_companies/get_by_account/customer_account_id.json' & while calling a method you need to pass the customer_account_id, from {customer_account_id: customer_account_id} so that it would create an URL with customer_account_id=2 somtehing like that.

Service

'getByCustomerAccount':{
      method: 'GET', 
      //params: {customer_account_id: customer_account_id}, //removed it
      isArray:true, 
      url:'/customer_companies/get_by_account/:customer_account_id'
}

Controller

CustomerCompany.getByCustomerAccount({customer_account_id: customer_account_id + '.json'}, function(data){
    console.log(data);
    //$scope.list_of_delegates.push(data);
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

Just note that there is no need to pass in .json the way you suggested. I think you can simply leave it there inside the url
Feel free to edit answer..I'll accept that edit..and that will help others too who are in such a problem..
1

This worked for me.

controllers.js

CustomerCompany.getByCustomerAccount({customer_account_id: customer_account_id}, function(data){
        console.log(data);
        //$scope.list_of_delegates.push(data);
    }); 

services.js

app.factory('CustomerCompany', function($resource){
    return $resource('/customer_companies/:id.json', {id: "@_id"}, {
        'query':{method: 'GET', isArray:false},
        'getByCustomerAccount':{
            method: 'GET', 
            isArray:false, 
            url:'/customer_accounts/:customer_account_id/customer_companies.json'
        }
    });
});

What I changed:

  1. on the server side, I decided to use /customer_accounts/:customer_account_id/customer_companies to render the results.
  2. I realized that the data returned is always in object form, so I changed isArray in the $resource to false.
  3. I pass the params like the way the Pankaj Parkar suggested in the controller when I call getByCustomerAccount

Comments

0

params: {customer_account_id: customer_account_id},

The customer_account_id you try to assign here is not defined.

Try params: {customer_account_id: '@customer_account_id'},

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.