使用带有mssql驱动程序的NestJ、TypeORM时,我会出错。我是用邮递员打我的。
GET:http://localhost:5000/customer/6
我得到的错误是:
Nest 11704 - 05/25/2020,12:04:32 AM ExceptionsHandler错误:以下选项在FETCH语句中的无效用法。+3863 of : Error:下一个选项在FETCH语句中的无效用法。在新的(E:\nest-api\node_modules\typeorm\error\QueryFailedError.js:11:28) at E:\nest-api\node_modules\typeorm\driver\sqlserver\SqlServerQueryRunner.js:232:61 at _query,(E:\nest-api\node_modules\mssql\lib\base\request.js:409:25) at Request.tds.Request.err as userCallback at Request.callback (E:\nest-api\node_(E:\nest-api\node_modules\mssql\lib\base\request.js:409:25)\乏味\lib\(E:\nest-api\node_modules\tedious\lib\connection.js:2407:20) at Connection.dispatchEvent (E:\nest-api\node_modules\tedious\lib\connection.js:1279:15) at Parser.tokenStreamParser.on (E:\nest-api\node_modules\tedious\lib\connection.js:1072:14) at Parser.emit (events.js:182:13)
在服务中,我使用以下代码
@Injectable()
export class CustomerService {
// Using decorator @InjectRepository for dependency injection and injecting repository to service
constructor(
@InjectRepository(Customer)
private customerRepository : Repository<Customer>){}
async GetAllCustomer()
{
return await this.customerRepository.find();
}
async AddCustomer(data : customerDTO)
{
const cust = await this.customerRepository.create(data)
await this.customerRepository.save(cust);
return cust;
}
async GetCustomerbyID(id : string)
{
return await this.customerRepository.findOne({ id : parseInt(id)});
}
async UpdateCustomer(id: string ,data : Partial<customerDTO>)
{
await this.customerRepository.update({ id: parseInt(id)},data);
}
async DeleteCustomer(id : string)
{
return await this.customerRepository.delete({ id : parseInt(id)});
return {deleted : true};
}
}在客户的控制器类中,已编写为controller.ts
@Controller('customer')
export class CustomerController {
constructor(private customerService : CustomerService){}
@Get()
GetAllCustomer()
{
return this.customerService.GetAllCustomer();
}
@Post()
AddCustomer(@Body() data : customerDTO)
{
return this.customerService.AddCustomer(data);
}
@Get(':Id')
GetCustomerByID(@Param('id') id: string)
{
return this.customerService.GetCustomerbyID(id);
}
@Put(':id')
UpdateCustomer(@Param('id') id: string,@Body() data : customerDTO)
{
return this.customerService.UpdateCustomer(id, data);
}
@Delete(':Id')
DeleteCustomerbyID(@Param('id') id: string)
{
return this.customerService.DeleteCustomer(id);
}
}邮递员http://localhost:5000/customer/6中的请求URL
响应{ "statusCode":500,“消息”:“内部服务器错误”}
我只是通过日志记录: true和它是失败的原因是
at Parser.parser.on.token (E:\nest-api\node_modules\tedious\lib\token\token-stream-parser.js:37:14)
query: SELECT "Customer"."id" AS "Customer_id", "Customer"."name" AS "Customer_name", "Customer"."address" AS "Customer_address", "Customer"."phonenumber" AS "Customer_phonenumber", "Customer"."age" AS "Customer_age", "Customer"."isEmployeed" AS "Customer_isEmployeed" FROM "customer" "Customer" WHERE "Customer"."id" = @0 ORDER BY (SELECT NULL) OFFSET 0 ROWS FETCH NEXT 1 ROWS
ONLY -- PARAMETERS: ["6"]
query failed: SELECT "Customer"."id" AS "Customer_id", "Customer"."name" AS "Customer_name", "Customer"."address" AS "Customer_address", "Customer"."phonenumber" AS "Customer_phonenumber", "Customer"."age" AS "Customer_age", "Customer"."isEmployeed" AS "Customer_isEmployeed" FROM "customer" "Customer" WHERE "Customer"."id" = @0 ORDER BY (SELECT NULL) OFFSET 0 ROWS FETCH NEXT
1 ROWS ONLY -- PARAMETERS: ["6"]如果我遗漏了任何细节,请告诉我。
发布于 2020-05-27 15:51:14
我已经在我的应用程序中使用了下面的解决方法。如果有人得到更好的解决方案,请张贴。
而不是使用下面的代码获取一个记录。
async GetCustomerbyID(id : string)
{
return await this.customerRepository.findOne({ id : parseInt(id)});
}用那个。
async GetCustomerbyID(id : number)
{
return await this.customerRepository.find({where : {id : id}});
}现在,在使用findOne时,我使用了find with 子句,您可以根据需要放置过滤器。谢谢你们的努力..。
https://stackoverflow.com/questions/61990797
复制相似问题