Node.js 端点冲突
每当我执行以下端点时,我都会得到一个ID类型错误Node.js 端点冲突,node.js,typescript,loopbackjs,loopback4,Node.js,Typescript,Loopbackjs,Loopback4,每当我执行以下端点时,我都会得到一个ID类型错误 Request GET /lists/45 failed with status code 500. error: invalid input syntax for type uuid: "45" 有问题的端点: // @get('/blabla/{color}', { //---> Works! @get('/lists/{color}', { //---> Error! response
Request GET /lists/45 failed with status code 500. error: invalid input syntax for type uuid: "45"
有问题的端点:
// @get('/blabla/{color}', { //---> Works!
@get('/lists/{color}', { //---> Error!
responses: {
'200': {
description: 'Query all lists by color',
},
},
})
async getListByColor(@param.path.string('color') color: number): Promise<number> {
return this.listsRepository.dataSource.execute("SELECT * FROM public.lists as li WHERE li.color = " + color);
}
/@get('/blabla/{color}',{/->有效!
@获取('/lists/{color}',{/->错误!
答复:{
'200': {
description:'按颜色查询所有列表',
},
},
})
异步getListByColor(@param.path.string('color')颜色:编号):承诺{
返回此.listsRepository.dataSource.execute(“从public.lists中选择*作为li,其中li.color=“+color”);
}
另一个端点可以正常工作:
@get('/lists/{id}', {
responses: {
'200': {
description: 'Lists model instance',
content: {
'application/json': {
schema: getModelSchemaRef(Lists, {includeRelations: true}),
},
},
},
},
})
async findById(
@param.path.string('id') id: string,
@param.filter(Lists, {exclude: 'where'}) filter?: FilterExcludingWhere<Lists>
): Promise<Lists> {
return this.listsRepository.findById(id, filter);
}
@get('/lists/{id}'{
答复:{
'200': {
description:'列出模型实例',
内容:{
“应用程序/json”:{
schema:getModelSchemaRef(列表,{includeRelations:true}),
},
},
},
},
})
异步findById(
@param.path.string('id')id:string,
@param.filter(列出,{exclude:'where'})筛选器?:FilterExcludingWhere
):承诺{
返回此.listsRepository.findById(id,筛选器);
}
REST路由器无法区分两个端点,/lists/{color}
和/lists/{id}
,因为这两个路径将解析为/lists/{任意字符串}
。
因此,路由器可能将流量路由到需要UUID数据类型id的错误端点。因此,出现数据库引擎级错误
也请注意,第一个例子易受SQL注入攻击的影响。请考虑使用它。
以供将来参考,此问题已在别处发布:如果此问题尚未得到解答,请检查上面的链接。参见如何加入松弛通道。