Search code examples
node.jsnestjstypeorm

TypeORM : invalid usage of the option next in the fetch statement. entity framework


I am getting error using NestJs, TypeORM with mssql driver. I am getting it by hitting by using the postman.

GET : http://localhost:5000/customer/6

I am getting the Error AS :

[Nest] 11704 - 05/25/2020, 12:04:32 AM [ExceptionsHandler] Error: Invalid usage of the option NEXT in the FETCH statement. +3863ms QueryFailedError: Error: Invalid usage of the option NEXT in the FETCH statement. at new QueryFailedError (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] (E:\nest-api\node_modules\mssql\lib\tedious\request.js:471:15) at Request.callback (E:\nest-api\node_modules\tedious\lib\request.js:56:14) at Connection.endOfMessageMarkerReceived (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)

In service i am using the below code


   @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};
    }

}


In the controller class of the customer has written as 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);
}

}

request URL in postman http://localhost:5000/customer/6

response { "statusCode": 500, "message": "Internal server error" }

i just by logging : true and it is failing due to

 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"]

Please let me know if i missed any details.


Solution

  • I have used the below workaround for my application. If any one get better solution than that please post.

    Instead of using below code for fetching one record.

    async GetCustomerbyID(id : string)
        {
            return await this.customerRepository.findOne({ id : parseInt(id)});
        }
    

    use that.

     async GetCustomerbyID(id : number)
        {
            return await this.customerRepository.find({where : {id : id}});
        }
    

    Now insted of using the findOne i am using find with where clause and you can put the filter based on your need. Thanks all for your effort...