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?

Obed Lorisson
  • 449
  • 3
  • 8
  • 22

1 Answers1

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?

  • 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}}' – Obed Lorisson Jul 04 '17 at 18:29
  • 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? –  Jul 04 '17 at 20:05
  • im using Baas as backend this is sql , this is the query to pull the object by id – Obed Lorisson Jul 04 '17 at 21:37
  • 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? –  Jul 04 '17 at 22:10
  • i'm not doing any other processing , the query is to pull up all the ids in the array to retrieve the associated objects – Obed Lorisson Jul 06 '17 at 01:59
  • In standard SQL Server syntax, the query would need to be `select * from videos where id in (102,110,112,125)`. Is your BaaS SQL Server, or some other backend? If you're using a Document DB, the syntax would be different, so it would be helpful to identify it. –  Jul 06 '17 at 11:47
  • yeah i did try the way that you said , the query only pull up the first id from the collection .i'm trying to find another solution so far none seems working ., – Obed Lorisson Jul 07 '17 at 01:01