我一直在使用 Loopback v4 包的早期版本,并且 RestBindings.Http.Request 绑定一直运行良好,直到最近的升级之一(不太确定它何时发生)。
我仍然能够获取大多数属性的值,但不能获取查询属性。即使是最基本的项目,使用默认的 ping 控制器,查询属性仍然是空的。以下是我的代码、查询和响应的示例:
import { Request, RestBindings, get, ResponseObject } from '@loopback/rest';
import { inject } from '@loopback/context';
export class PingController {
constructor(@inject(RestBindings.Http.REQUEST) private req: Request) { }
// Map to `GET /ping`
@get('/ping')
ping(): object {
// Reply with a greeting, the current time, the url, and request headers
return {
query: 'Query response: ' + this.req.query.start,
greeting: 'Hello from LoopBack',
date: new Date(),
url: this.req.url,
headers: Object.assign({}, this.req.headers),
};
}
}
查询:localhost:3000/ping?start=2018-08-25&end=2018-09-09&user=larsm
输出:
{
"query": "Query response: undefined",
"greeting": "Hello from LoopBack",
"date": "2018-11-27T23:21:53.142Z",
"url": "/ping?start=2018-08-25&end=2018-09-09&user=larsm",
"headers": {
"host": "localhost:3000",
"connection": "keep-alive",
"cache-control": "max-age=0",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding": "gzip, deflate, br",
"accept-language": "nb-NO,nb;q=0.9,en-GB;q=0.8,en;q=0.7,no;q=0.6,nn;q=0.5,en-US;q=0.4" } }