2

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.

Jasper Rosiers
  • 277
  • 2
  • 5
  • 16
  • Where is `@Query()` being imported from? Is it by chance from `@nestjs/graphql`? And are your nest common, core, and platform packages on the same version? – Jay McDoniel Jun 02 '20 at 21:56
  • It's all imported from Common. `import { Controller, Get, Body, HttpException, Post, Query } from "@nestjs/common";`. Except for typeORM, they're all on version 6.7.2, typeORM is on 7.0.0 – Jasper Rosiers Jun 02 '20 at 21:58
  • Do you have some reproduction of this? – Jay McDoniel Jun 03 '20 at 14:10
  • I unfortunately cannot share the files due to them being related to my work. I have however found a 'solution', which i'll share in an answer below. – Jasper Rosiers Jun 03 '20 at 16:32

2 Answers2

2

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

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • I updated all Nest packages to latest (7.1.2 for common, core and platform), to no avail. Using your getHello function, the query log says `[Function: getQuery]`, when I return it, postman gives me the following: `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 || ''; }` – Jasper Rosiers Jun 02 '20 at 22:19
  • Are you using Fastify or Express? – Jay McDoniel Jun 02 '20 at 22:22
  • @nestjs/platform-express:"7.1.2" – Jasper Rosiers Jun 02 '20 at 22:30
1

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.

Jasper Rosiers
  • 277
  • 2
  • 5
  • 16
  • 1
    Looks like somehow the resitfy request is what was being used, which is why query became the `getQuery()` function. Not really sure how that happened without seeing what was going on in `main.ts`, but glad you got it resolved. – Jay McDoniel Jun 03 '20 at 16:42
  • Thanks for helping me look into the issue! – Jasper Rosiers Jun 03 '20 at 16:47