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.