How to Generate ngResource url with two parameters - angularjs

i'm trying to generate this url using ngResource in angularjs:
http://url_api/contact/58dc70f18e029b1338a97abc/address/58e3e3988e029a1aec5ad465
but instead i'm getting this:
http://url_api/contact/58dc70f18e029b1338a97abc/address?address_id=58e3e3988e029a1aec5ad465
This is my factory
factory('Addresses', function($resource) {
return $resource(url_base + 'contact/:id/address',{},{
get: {
method: 'GET',
isArray: true,
url: url_base + 'contact/:id/address'
},
save:{
method: 'POST'
},
delete:{
method: 'DELETE',
url: url_base + 'contact/:id/address/:addressId',
// params:{
// id: '#_id',
// addressId: '#_addressId'
// }
}
});
})
and this is how i call it later
Addresses.delete({id:$scope.id_contact,address_id:address_id_delete})
Please could anyone help me? I'm in a hurry

You need to mention your params in the empty object you have. Like this:
factory('Addresses', function($resource) {
return $resource(url_base + 'contact/:id/address',{
id: '#id',
addressId: '#addressId'
},{
get: {
method: 'GET',
isArray: true,
url: url_base + 'contact/:id/address'
},
save:{
method: 'POST'
},
delete:{
method: 'DELETE',
url: url_base + 'contact/:id/address/:addressId',
// params:{
// id: '#_id',
// addressId: '#_addressId'
// }
}
});
})

The parameters in the url template need to match parameters in the call:
delete:{
method: 'DELETE',
url: url_base + 'contact/:id/address/:addressId',
// params:{
// id: '#_id',
// addressId: '#_addressId'
// }
}
//WRONG address_id
//Addresses.delete({id:$scope.id_contact,address_id:address_id_delete})
//RIGHT addressId
Addresses.delete({id:$scope.id_contact,addressId:address_id_delete})

Related

PUT request sending params via URL

I have a order resource that looks like so.
.factory('Order', order)
order.$inject = ['$resource', "ApiEndpoint"];
function order($resource, ApiEndpoint) {
return $resource(ApiEndpoint.url + 'orders.json', {}, {
create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'},
update: {method: 'PUT'},
edit: {method: 'GET', url: ApiEndpoint.url + 'orders/edit.json'},
remove_item: {method: 'GET', url: ApiEndpoint.url + 'orders/remove_item.json'},
});
}
When I run Order.update like so
var params = {
order: {
line_items_attributes: {0: {quantity: 2, id: 1}}
},
order_id: 3
};
Order.update(params, function (resp, respHeaders) {
console.log("response headers", respHeaders());
console.log("change quantity resp", resp);
})
I also tried this:
Order.update({}, params, function (resp, respHeaders) {
console.log("response headers", respHeaders());
console.log("change quantity resp", resp);
})
The params sent to the server end up being inside the URL. For example this was one of the urls the server received
path="/api/mobile/orders.json?order=%7B%22line_items_attributes%22:%7B%220%22:%7B%22quantity%22:8,%22id%22:356265%7D%7D%7D"
I should also note that the method being received by the server is an OPTIONS request. The server has been set up to handle this.
Since I'm sending a PUT request why is $resource delivering the params via the URL and not part of the payload?
from the docs:
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
the payload is the second argument, so try with this code
var params = {
order: {
line_items_attributes: {0: {quantity: 2, id: 1}}
},
order_id: 3
};
Order.update({}, params, function (resp, respHeaders) {
console.log("response headers", respHeaders());
console.log("change quantity resp", resp);
})
just add an empty object as first parameter to the update method.
have also a look to he section related to custom put requests
If you are updating an order then you should speficy the order id so that service can now which order is going to be updated
function order($resource, ApiEndpoint) {
return $resource(ApiEndpoint.url + 'orders.json/:orderid', {}, {
create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'},
update: {method: 'PUT',params : {orderid : '#order_id'},
edit: {method: 'GET', url: ApiEndpoint.url + 'orders/edit.json'},
remove_item: {method: 'GET', url: ApiEndpoint.url + 'orders/remove_item.json'},
});
}
and then your call
Order.update(params, function (resp, respHeaders) {
console.log("response headers", respHeaders());
console.log("change quantity resp", resp);
})

How to get the value from a resolved promise

I am new to Angular and having hard time to understand how to get the value from a resolved promise. I used $q.all([element1.$promise, e2.$promise]). I got the element1 which is a json object. For, element2 which is a scalar containing 2.0. I tried element[1] just like element[0] but it contains no property, so I don't know how to get the value. I tried .val, .data, etc. I know both the web api and angular are getting 2.0.
resolve: {
accountResource: "accountResource",
account: function(accountResource, $stateParams, $q) {
var info = accountResource.get({ accountId: $stateParams.accountId });
var currentBalance = accountResource.getCurrentBalance({ accountId: $stateParams.accountId });
return $q.all([info.$promise, currentBalance.$promise]);
}
vm.account = account[0];
vm.currentBalance = account[1];
resource function
function accountResource($resource) {
var webApiUrl = "https://localhost:44301";
return $resource(webApiUrl + '/api/account/:accountId', {}, {
get: {
method: 'GET'
},
query: {
method: 'GET',
isArray: true,
url: webApiUrl + '/api/account/search/:queryMethod/:queryString'
},
getCurrentBalance: {
method: 'GET',
url: webApiUrl + '/api/account/getcurrentbalance/:accountId'
}
});
}
You can call $q.all likes this (with dictionary instead of array):
return $q.all({info: info.$promise, currentBalance: currentBalance.$promise})
And after that get information in this way:
vm.account = account.info;
vm.currentBalance = account.currentBalance;
Try to rewrite your resource as this:
function accountResource($resource) {
var webApiUrl = "https://localhost:44301";
// Add second parameter {accountId: "#accountId"}
return $resource(webApiUrl + '/api/account/:accountId', {accountId: "#accountId"}, {
get: {
method: 'GET'
},
query: {
method: 'GET',
isArray: true,
url: webApiUrl + '/api/account/search/:queryMethod/:queryString'
},
getCurrentBalance: {
method: 'GET',
url: webApiUrl + '/api/account/getcurrentbalance/:accountId'
}
});
}
Updated
You can add transformResponse property to your getCurrentBalance function in accountResoure. And transform your value from API to json format.
getCurrentBalance: {
method: 'GET',
url: webApiUrl + '/api/account/getcurrentbalance/:accountId',
transformResponse: function (balanceFromApi) {
return { balance: balanceFromApi };
}
}
And finally you can get information, likes this:
vm.account = account.info;
vm.currentBalance = account.balance;
I tested it with $httpBackend service and everything is fine now. Hope this helps.

TypeError: Object #<Resource> has no method 'push'

I have searched other solutions but it doesn't work, solution was to put isArray: true, but it doent work. I am working with objects btw. I am getting this message in console, but everything works, data has been send to my backend api. How to remove that error ?
app.factory('Category', function($resource, $http) {
return $resource('http://localhost/api/v1/category/', {id: "#id"},
{
update: {
method: 'POST',
isArray: false
},
save: {
method: 'PUT'
},
query: {
method: 'GET',
params: {id: '#id'},
isArray: false
},
create: {
method: 'POST',
headers: {'Content-Type': 'application/json'}
},
drop: {
method: 'DELETE',
params: {id: '#id'}
}
}
);
});
Here is my function
$scope.createInventory = function(){
Inventory.create($scope.newinfo);
$scope.info.push(Inventory);
};
$scope.info not a array. You must specify $scope.info to array, then you can push any values to $scope.info.
Try this
$scope.info=[];
$scope.createInventory = function(){
Inventory.create($scope.newinfo);
$scope.info.push(Inventory);
};

delete an object using resource by id

I want to delete an object selected by id, but when I press "delete" all objects are removed.
If I specify Inventory.drop({id: 2} it works, but using Inventory.drop({id: '#id'}) it doesn't. Why?
$scope.deleteInv = function () {
Inventory.drop();
};
My template:
<td>X</td>
Here is my factory:
app.factory('Inventory', function($resource, $http) {
return $resource('http://someweb.com/api/v1/inventory/:id', {id: '#id'},
{
update: {
method: 'POST',
params: {id: '#id'},
isArray: false
},
save: {
method: 'PUT'
},
query: {
method: 'GET',
params: {id: '#id'},
isArray: false
},
create: {
method: 'POST'
},
drop: {
method: 'DELETE',
params: {id: '#id'}
}
}
);
});
Because your #id is undefined.
Where you assign values for that? I think you does't.
So you need to change that
params: {id: $scope.YourId}
and you need to should be pass any values for $sope.YourId
and also You need define $sope in your factory app.factory('Inventory', function($resource, $http,**$scope**) {

Generate Symfony Url in angular resource using FosJsBundle

I want genereate url in angular resource using FOSJSBundle.
This is the code that need FOSJSBundle to generate url:
Routing.generate('get_constructionprivateinformation_industrialists', {id: id}, false));
This is the service code that use resource:
services.factory('IndustrialistsFactory', function ($resource) {
return $resource(
'/app.php/api/v1/constructionprivateinformations/:id/industrialists',
{id: '#id'},
{
query: { method: 'GET', isArray: true },
create: { method: 'POST'}
}
)
});
This code works but I want generate url using FOSJSBundle, then If I do this code not works because Routing.generate have not access to id.
I proved this code but not works:
services.factory('IndustrialistsFactory', function ($resource) {
return $resource(
Routing.generate('get_constructionprivateinformation_industrialists', {id: id}),
{id: '#id'},
{
query: { method: 'GET', isArray: true },
create: { method: 'POST'}
}
)
});
What Can I do?
Thanks

Resources