0

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?

2
  • Do you use TypeScript? If so, please add the appropiate tag. Commented Jul 4, 2017 at 18:00
  • sure you can do that. Is your server code set up to handle that type of request? Commented Jul 4, 2017 at 18:10

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

7 Comments

the prob is the array that hold the value is dynamic ,and i cannot hard coded it . here's the server code for the query SELECT * from videos WHERE id = '{{id}}'
You don't have to hardcode it. You can loop through the dynamic array and construct the querystring that way. Maybe I'm misunderstanding you on that. For the SQL, is the ID in the template brackets the same value as the ID in the stringified JSON? What does this SQL evaluate too after the template replacement? It is MySQL, NoSQL, SQL Server?
im using Baas as backend this is sql , this is the query to pull the object by id
Right, but your JS is attempting to pass in an key/value pair where the key is "id" and the value is an array of numbers (ids of the favorites), while your SQL looks like it's just selecting one record for a single ID. In an RMDBS you'd use an in clause. Does your BaaS support this? Otherwise you'd have to dynamically create the SQL with or clauses in the where. Or is there some other processing you're doing once it hits the server (before the DB) that you haven't posted?
i'm not doing any other processing , the query is to pull up all the ids in the array to retrieve the associated objects
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.