JasperR
JasperR

Reputation: 369

How to get parameters out of Query in NestJS

I want to be able to get a license plate from my query parameters, but it doesn't seem to work, whatever way I write my code, so I'm getting quite confused why it's not working.

Here's the expected behaviour: when using the following GET request: http://localhost:3978/licenseplate/getleasingcompany?licenseplate=1-WMW-478 I want to extract the licenseplate parameter.

Here's my current code:

@Get('getleasingcompany')
async getLeasingCompany(@Query('licenseplate') licenseplate: string) {
    console.log(licenseplate);
}

This logs licenseplate as undefined when trying in postman.

I also tried variants of this code, such as:

@Get('getleasingcompany')
async getLeasingCompany(@Query() query) {
    console.log(query);
}

This logged query as [Function: getQuery], which I have no idea how to handle (query.licenseplate is undefined)

Another option is found in a Stackoverflow explanation here, in which Kim uses path parameters. This did work for me, but isn't behaviour my program can use, unfortunately.

Could anyone explain what I'm doing wrong? Thanks!

EDIT 1: After a few comments, I updated my packages to version 7.1.2, to no avail. Returning the query to postman gives following output:

function getQuery() {
// always return a string, because this is the raw query string.
// if the queryParser plugin is used, req.query will provide an empty
// object fallback.
return this.getUrl().query || '';
}

EDIT 2: Tried going the Express route by importing Request as follows: import { Request } from "express";. The code now looks like this:

@Get('getleasingcompany')
  async getLeasingCompany(@Req() request: Request) {
    console.log(request);
}

A part of this log does show me that the query saved the variable:

originalUrl: '/licenseplate/getleasingcompany?licenseplate=1-WMW-478',
  _parsedUrl:
   Url {
     protocol: null,
     slashes: null,
     auth: null,
     host: null,
     port: null,
     hostname: null,
     hash: null,
     search: '?licenseplate=1-WMW-478',
     query: 'licenseplate=1-WMW-478',
     pathname: '/licenseplate/getleasingcompany',
     path: '/licenseplate/getleasingcompany?licenseplate=1-WMW-478',
     href: '/licenseplate/getleasingcompany?licenseplate=1-WMW-478',
     _raw: '/licenseplate/getleasingcompany?licenseplate=1-WMW-478' },

However, when logging the request.query, I still get [Function: GetQuery] as the result.

Upvotes: 3

Views: 15282

Answers (2)

JasperR
JasperR

Reputation: 369

I haven't found why the program didn't work as intended, I have however fixed the issue simply by starting a new Nest project. When I noticed this project did work as intended, I copied over all the files except for package.json and reinstalled all the necessary dependencies one by one. I only deleted 1 dependency, restify, which I ended up not needing. It worked with the package though, so that wasn't the issue.

Due to these findings, I suppose it must have been a versioning issue, though I'm still not sure.

Upvotes: 1

Jay McDoniel
Jay McDoniel

Reputation: 70111

Nothing looks wrong in your code. On Nest v7 using the following controller and curl I get what you'd expect

import { Controller, Get, Query } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(@Query() query: Record<string, any>): string {
    console.log(query);
    return this.appService.getHello();
  }
}
curl http://localhost:3000/\?queryparam\=something
[Nest] 46471   - 06/02/2020, 2:55:11 PM   [NestFactory] Starting Nest application...
[Nest] 46471   - 06/02/2020, 2:55:11 PM   [InstanceLoader] AppModule dependencies initialized +12ms
[Nest] 46471   - 06/02/2020, 2:55:11 PM   [RoutesResolver] AppController {}: +15ms
[Nest] 46471   - 06/02/2020, 2:55:11 PM   [RouterExplorer] Mapped {, GET} route +9ms
[Nest] 46471   - 06/02/2020, 2:55:11 PM   [NestApplication] Nest application successfully started +9ms
{ queryparam: 'something' }

My only guess is some mismatched version of Nest

Upvotes: 2

Related Questions