Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over @Query() object in Nest js

Is there any way, how to iterate over object which we got in controller by @Query() anotations?

We have dynamic count and name of query parameters in GET, so we need to take whole @Query() object and iterate over them to know what paramas we exactly have.

But if I want to iterate over that object I got error that object is not iterable.

Any idea how to do that?

like image 330
Dominik Zatloukal Avatar asked Dec 14 '22 12:12

Dominik Zatloukal


1 Answers

You can use Object.keys() to get an array of the keys of the query object. You can then iterate over this array of keys:

@Get()
getHello(@Query() query) {
  for (const queryKey of Object.keys(query)) {
    console.log(`${queryKey}: ${query[queryKey]}`);
  }
}
like image 90
Kim Kern Avatar answered Dec 21 '22 11:12

Kim Kern