我一直在使用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" } }
答案 0 :(得分:1)
所以我发现以上问题是由@ loopback / rest库中的有意更改引起的,即仅处理预定义的查询参数。
这意味着您需要在控制器中专门定义它们,例如就像这个简单的例子:
@get('/larsm')
greet(
@param.query.string('start') start: string,
@param.query.string('end') end: string,
@param.query.string('name') name: string,
) {
return 'start: ' + start + ', end: ' + end + ', name: ' + name;
}
}
由于在很多情况下这并不总是理想的,所以我确实向Loopback团队提出了issue的建议,看来他们决定还原最近的更改。
答案 1 :(得分:1)
此问题将在下一版本中解决,请参见https://github.com/strongloop/loopback-next/pull/2089