0

I'm trying to make a get/post request to an Express+Node server using URLSearchParams from an Angular 4 app and no matter what I do I can´t get the data in the server.

I'm doing something like this

let params: URLSearchParams = new URLSearchParams();
params.set('company', company);
return this.authHttp
      .get(`${api}/${id}`, { search: params, params })

The second parameter for http.get is RequestOptionsArgs, which is

interface RequestOptionsArgs { 
  url?: string|null
  method?: string|RequestMethod|null
  search?: string|URLSearchParams|{[key: string]: any | any[]}|null
  params?: string|URLSearchParams|{[key: string]: any | any[]}|null
  headers?: Headers|null
  body?: any
  withCredentials?: boolean|null
  responseType?: ResponseContentType|null
}

so this should be working fine, but when I try to get req.query.company in the server it turns out that req.query is undefined. In req.params I can find the id that I send in the url, but no company either.

Any idea about what I might be doing wrong?

Thanks

3
  • check my answer for angular 4 . Commented Aug 16, 2017 at 9:53
  • Can you post the code for authhttp service? Commented Aug 16, 2017 at 10:17
  • The question is too broad and lacks stackoverflow.com/help/mcve . You should check network tab and provide this information. If params exist in the request then Angular part is irrelevant, and this is purely Node question. Otherwise Node part is irrelevant, and this is purely Angular question. Commented Aug 16, 2017 at 11:20

1 Answer 1

1

Edit : updated my answer with the simplified version

let params: URLSearchParams = new URLSearchParams();
return this.authHttp
  .get(`${api}/${id}`, { params: { company: company } });
Sign up to request clarification or add additional context in comments.

4 Comments

I changed set for append but nothing happened; I guess this is used when you want to add more than one element to the params; in my case I'm only using one, so it makes sense
Did you change it to {` search: params }`? How are you parsing the search param at Node server?
{ search: params, params } or { search: params } should work the same, I'm just passing the URLSearchParams in 2 different fields. In the node server I use req.params and req.query to get the data.
Updated my answer .. can you add the code for authHttp.get

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.