Reputation: 3424
employee.service.js
angular.
module('core.employee').
factory('Employee', ['$resource',
function($resource) {
return $resource('http://127.0.0.1:8007/api/business/:businessId/employees/:employeeId', {}, {
query: {
method: 'GET',
params: {businessId: '', employeeId: ''},
isArray: true
},
create: {
method: 'POST',
params: {businessId: '', employeeId:''}
},
});
}
]);
And inside my controller:
self.employees = Employee.query({businessId: $routeParams.businessId}, function(employees) {
});
self.create = function(){
Employee.create({
businessId: $routeParams.businessId,
email: self.email,
access: self.access.value
}).$promise.then(function(employee) {
self.errorMsg = "";
self.employee = employee;
}, function(errResponse) {
console.log($routeParams.businessId);
self.errorMsg = errResponse["data"];
});
}
currentUrl: http://127.0.0.1:8007/businesses/1/employees/create
I was able to query
employees. But unable to create
. The error thrown is:
angular.js:13018 POST http://127.0.0.1:8007/api/business//employees/ 404 (Not Found)
The actual url for posting data should be: http://127.0.0.1:8007/api/business/1/employees/
Here the businessId
appears to have not passed to the resource. But inside the function handing the error message: console.log($routeParams.businessId)
gives 1
.
The route inside config
:
when('/businesses/:businessId/employees/create', {
template: '<employee-create></employee-create>'
}).
Upvotes: 0
Views: 27
Reputation: 3664
You haven't properly wired up your $resource
to map your request parameters to the request path.
$resource(
'http://127.0.0.1:8007/api/business/:businessId/employees/:employeeId',
// Set up default parameters to extract values from the corresponding
// properties when the resource is used
{ businessId: '@businessId', employeeId: '@employeeId' },
{
query: {
method: 'GET',
isArray: true
},
create: {
method: 'POST',
},
});
Failing to map the parameters causes the resultant request path to have fragments absent.
The documentation for the default parameters arguments mentions this mapping behaviour:
If the parameter value is prefixed with
@
, then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling actions with a request body).
Upvotes: 1