【问题标题】:TypeORM : invalid usage of the option next in the fetch statement. entity frameworkTypeORM : 在 fetch 语句中对选项 next 的无效使用。实体框架
【发布时间】:2020-09-11 09:44:33
【问题描述】:

我在使用 NestJs、TypeORM 和 mssql 驱动程序时遇到错误。 我是用邮递员打的。

获取:http://localhost:5000/customer/6

我收到错误 AS:

[Nest] 11704 - 2020 年 5 月 25 日上午 12:04:32 [ExceptionsHandler] 错误:FETCH 语句中选项 NEXT 的使用无效。 +3863 毫秒 QueryFailedError:错误:FETCH 语句中选项 NEXT 的使用无效。 在新的 QueryFailedError (E:\nest-api\node_modules\typeorm\error\QueryFailedError.js:11:28) 在 E:\nest-api\node_modules\typeorm\driver\sqlserver\SqlServerQueryRunner.js:232:61 在 _query (E:\nest-api\node_modules\mssql\lib\base\request.js:409:25) 在 Request.tds.Request.err [as userCallback] (E:\nest-api\node_modules\mssql\lib\tedious\request.js:471:15) 在 Request.callback (E:\nest-api\node_modules\tedious\lib\request.js:56:14) 在 Connection.endOfMessageMarkerReceived (E:\nest-api\node_modules\tedious\lib\connection.js:2407:20) 在 Connection.dispatchEvent (E:\nest-api\node_modules\tedious\lib\connection.js:1279:15) 在 Parser.tokenStreamParser.on (E:\nest-api\node_modules\tedious\lib\connection.js:1072:14) 在 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};
    }

}


在客户的控制器类中写成 控制器.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

响应 { “状态代码”: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"]

如果我遗漏了任何细节,请告诉我。

【问题讨论】:

  • 您好,您能告诉我们客户存储库的方法实现吗?
  • 你能在邮递员中告诉我你的要求吗
  • 不是 GET 而是 PUT
  • 这不是你要找的答案,顺便提一下,因为在你的控制器中你应该 awaitthis.customerService.UpdateCustomer 返回一些东西。回到你的问题:你可以试试await this.customerRepository.update(parseInt(id),data); 吗?
  • 请在底部找到方法实现和 Postman 请求,它是 Get with id for specific customer。 @0xCAP 我正在将其解析为存储库中的整数。

标签: node.js nestjs typeorm


【解决方案1】:

我为我的应用程序使用了以下解决方法。如果有人得到比这更好的解决方案,请发布。

而不是使用下面的代码来获取一条记录。

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 我正在使用 findwhere 子句,您可以根据需要放置过滤器。 感谢大家的努力...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多