I have the following ids:
var myIds = ['somelongId', 'someotherlongid'];
I want to get only the records that correspond from mongo (I am using angular-fullstack) using $resource.
My factory
angular.module('myApp')
.factory('MyFactory',function ($resource) {
return $resource('/api/things/:id');
});
My faulty code returns all the records
MyFactory.query(myIds);
You should query correctly, by passing id parameter in it. You can't pass array object inside query, if you try to pass that then that will make stringify that array to [Object Object] & it will throw an error.
Instead I'd suggest you to pass any one of id to it.
MyFactory.query({ id: myIds[0] });
Edit
If you wanted to pass multiple ids in a query statement then pass it as , separated string and then do split out that key on server side. In that way you could pass multiple array to server & will get array of response(.query will make thing done).
MyFactory.query({ id: myIds.join(',') });
This will create url as
http://localhost/api/things/somelongId,someotherlongid
Related
I'am trying to get paramter from url like that:
index.html#!/forum/page?1-something
the number that what i'am trying to get
both $location.url(); and $location.absUrl(); get the whole url
do I have to use split and how to do that?
Using $location.search() you can get all query parameters.
by iterating an object you can get all key and value as well.
I used to iterate object using for-in loop. here $location.search() is an object itself so you can iterate it as:
for(let key in $location.search()){
console.log("key: "+key+" value: "+$location.search()[key]);
}
we can also append query parameters using $location.search() by just passing parameters to it. this can be done as:
let obj = {site:'stack overflow', exp: 'happy coding'}
and this obj object can be passed to $location.search() as:
$location.search(obj);
I have an array of id let's say
favorites = [102,110,112,125]
I want to retrieve the corresponding object for each id by passing it to query string like this :
public getFavorites(favs){
let favorites = favs.join();
let encodedUrl = encodeURIComponent(JSON.stringify({"id": favorites }));
return this.http.get(this.api_url + '/1/query/data/getFavs?parameters='+encodedUrl, {
headers: this.authHeader
})
.retry(3)
.map(res => res.json());
}
The problem is only one object appear in my html template and also in the console. What is the best way for me to pass an array of value to a URL as parameters in order to retrieve the associated objects?
You can pass multiple parameter values with the same name over the querystring. Does that help you? For example, here's a snippet:
this.api_url + '/1/query/data/getFavs?id=101&id=110
Here is another answer that has some more info on this.
If you have to send the ID's over in a serialized manner, consider posting the JSON instead of using the GET method. If you're trying to maintain adherence to REST verb standards by making it a get call, can you post the server code?
Let see my Angularjs code:
My function 'getequipselected' from my 'EmpApi' factory request data of my webservice and save it in response:
EmpApi.getequipselected(idequip).success(function (response) {
$scope.editequipamento = response;
console.log(response);
Its perfectly working, it printed on console log browser: Array [ Object ]
If a click on 'Objetc', I can see all parameters: idEquipamento, TAG, idDiretoria, idFabricante ...
So i trieded to print the $scope:
console.log($scope.editequipamento);
I printed it to check, it was the same of console.log(response)
The problem is here: I tried to print it:
console.log($scope.editequipamento.TAG);
I tried to print one parameter from object but in gave me 'undefined'
I trieded it because I'd like to pass this value to my html input, using ng-model="editequipamento.TAG", but the input is also undefined.
the response was array not an object so u have to access the first object of that array.
console.log($scope.editequipamento[0].TAG);
Try editequipamento[0].TAG. I think your object is inside of array of list.
You need to use ng-repeat, if you have bulk array
I have seen 100 examples of passing an ID into $resource.get() in order to query information out of a back-end in Angular. What I have not been able to find is how to pass a complex object.
If I have a table of objects to return, and I wish to run a search against them using multiple items of filter, I need to pass those items as parameters or as one complex parameter. For example, say I have a table of people's names and their cities, states, etc. I want to be able to say something like this:
var myResource = $resource(url);
myResource.get({name : "Mike", state : "Texas"});
The return may be a single row or multiple rows. But the point is how do I get the parameters off to the API call?
The way I have other methods set up that are simpler is by creating a repository in which I return like so:
return resource('/api/broker/productionInfo/');
Then in my API I do this (after the [RoutePrefix("api/broker")] setup:
[HttpGet]
[Route("productionInfo")]
public IHttpActionResult GetProductions()
{}
That's all awesome but I want to be able to add the search criteria in the repository call and then in the API method (i.e. extract from a querystring or however it is to be passed).
If I understand what you are asking correctly, you just want to pass additional parameters into an angular resource get request. It is as simple as what you have already suggested:
resource.get({id: 1, custom_param_1: "param1", custom_param_2: "param2"});
This would result in an http request that looks like this:
/resource/1?custom_param_1=param1&custom_param_2=param2
You can then extract these parameters on the API side of things.
Something to note is that get requests have a maximum length, and if you are attaching lots of different parameters to the request, it may be better to use a post or put instead.
The only thing I'm seeing that you're missing is a [FromUri] decorate attribute, in your GetProduction API method. Since Get supports only params binding through a query string (no body binding).
Your params:
options: {
StartDate: _startDate
EndDate: _endDate
TextSearch: "some search query....",
Page: 1,
PageSize: 25,
et...
}
Then, calling your repository from your controller:
repository.get(options).$promise.then(function (data) {
// data = response payload from backend
});
reposiroty
....
return resource('/api/broker/productionInfo/');
....
API
[HttpGet]
[Route("productionInfo")]
public IHttpActionResult GetProductions([FromUri] SearchCriteriaModel criteria) {
....
}
Hope that helps.
I need to create a fairly complex query string in Restangular.
http://vdmta.rd.mycompany.net//people?anr=Smith&attrs=givenName,displayName,name,cn
How do I do this?
So far I am OK getting as far as ?anr=Smith using this:
return Restangular.all('/people').getList({anr:searchTerm});
The last part attrs=x,y,x lets me control which attributes I want back in the search and could change per request I make.
Any advice appreciated.
Regards
i
You should be able to simply add another query parameter where the value is your comma separated list of attributes.
var attributes = ['givenName' , 'displayName']; // attributes you require for the request
var attributesAsString = attributes.join();
return Restangular.all('/people').getList({
anr : searchTerm,
attrs: attributesAsString
});