question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why use findOne to report errors

See original GitHub issue

**Issue type:**

[ ] question

Database system/driver: [ 4.0.4] mssql

TypeORM version:

[ ] latest

Steps to reproduce or a small repository showing the problem: I use SQL Server 2008 r2 database, find,findbyids can all be used properly, errors are reported when using findOne.

That’s how I use it.

import "reflect-metadata";
import { createConnection } from "typeorm";
import { PersonalModel } from  './entity/PersonalModel';
const bodyParser = require("koa-bodyparser");
const Koa = require("koa");
const Router = require("koa-router");
import Routes  from "./router";
const app = new Koa();
const router = new Router();
createConnection()
  .then(async connection => {
    Routes.forEach(route => {
      router[route.method](route.route, async (ctx, next) => {
        const result = await new route.controller()[route.action](ctx, next);
        ctx.type = "json";
        ctx.body = result;
      });
    });
    app.use(bodyParser())
    app.use(router.routes()).use(router.allowedMethods());
    app.listen(3000);
    let repository = connection.getRepository(PersonalModel);
    console.log(await repository.findOneOrFail());
  })
  .catch(err => console.log(err));

errors:

{ QueryFailedError: Error: Invalid usage of the option NEXT in the FETCH statement.
    at new QueryFailedError (C:\Users\86186\Documents\GitHub\BlogServerApi\src\error\QueryFailedError.ts:9:9)
    at C:\Users\86186\Documents\GitHub\BlogServerApi\src\driver\sqlserver\SqlServerQueryRunner.ts:221:37
    at _query (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\mssql\lib\base.js:1293:25)
    at Request.tds.Request.err [as userCallback] (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\mssql\lib\tedious.js:661:15)
    at Request._this.callback (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\tedious\lib\request.js:61:27)
    at Connection.endOfMessageMarkerReceived (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\tedious\lib\connection.js:1944:20)
    at Connection.dispatchEvent (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\tedious\lib\connection.js:1012:38)
    at Parser.<anonymous> (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\tedious\lib\connection.js:812:18)
    at Parser.emit (events.js:189:13)
    at Parser.EventEmitter.emit (domain.js:441:20)
  message:
   'Error: Invalid usage of the option NEXT in the FETCH statement.',
  code: 'EREQUEST',
  number: 153,
  lineNumber: 1,
  state: 2,
  class: 15,
  serverName: 'iZh5s5gb8yheuqZ',
  procName: '',
  originalError:
   { Error: Invalid usage of the option NEXT in the FETCH statement.
       at handleError (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\mssql\lib\tedious.js:554:19)
       at Connection.emit (events.js:189:13)
       at Connection.EventEmitter.emit (domain.js:441:20)
       at Parser.<anonymous> (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\tedious\lib\connection.js:618:16)
       at Parser.emit (events.js:189:13)
       at Parser.EventEmitter.emit (domain.js:441:20)
       at Parser.<anonymous> (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
       at Parser.emit (events.js:189:13)
       at Parser.EventEmitter.emit (domain.js:441:20)
       at addChunk (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\readable-stream\lib\_stream_readable.js:291:12)
     info:
      { number: 153,
        state: 2,
        class: 15,
        message: 'Invalid usage of the option NEXT in the FETCH statement.',
        serverName: 'iZh5s5gb8yheuqZ',
        procName: '',
        lineNumber: 1,
        name: 'ERROR',
        event: 'errorMessage' } },
  name: 'QueryFailedError',
  precedingErrors:
   [ { RequestError: Incorrect syntax near 'OFFSET'.
         at handleError (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\mssql\lib\tedious.js:556:15)
         at Connection.emit (events.js:189:13)
         at Connection.EventEmitter.emit (domain.js:441:20)
         at Parser.<anonymous> (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\tedious\lib\connection.js:618:16)
         at Parser.emit (events.js:189:13)
         at Parser.EventEmitter.emit (domain.js:441:20)
         at Parser.<anonymous> (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
         at Parser.emit (events.js:189:13)
         at Parser.EventEmitter.emit (domain.js:441:20)
         at addChunk (C:\Users\86186\Documents\GitHub\BlogServerApi\node_modules\readable-stream\lib\_stream_readable.js:291:12)
       code: 'EREQUEST',
       number: 102,
       lineNumber: 1,
       state: 1,
       class: 15,
       serverName: 'iZh5s5gb8yheuqZ',
       procName: '',
       originalError: [Error],
       name: 'RequestError' } ],
  query:
   'SELECT "PersonalModel"."id" AS "PersonalModel_id", "PersonalModel"."buildTime" AS "PersonalModel_buildTime", "PersonalModel"."updateTime" AS "PersonalModel_updateTime", "PersonalModel"."status" AS "PersonalModel_status", "PersonalModel"."name" AS "PersonalModel_name", "PersonalModel"."age" AS "PersonalModel_age", "PersonalModel"."gender" AS "PersonalModel_gender", "PersonalModel"."education" AS "PersonalModel_education", "PersonalModel"."occupation" AS "PersonalModel_occupation", "PersonalModel"."company" AS "PersonalModel_company", "PersonalModel"."centered" AS "PersonalModel_centered" FROM "Blog_Personal" "PersonalModel" ORDER BY (SELECT NULL) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY',
  parameters: [] }

need help, thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
vlapocommented, Jun 6, 2019

Sorry boys but SQL Server 2008 is not even supported by Microsoft. I cannot even find documentation but it is possible that OFFSET/FETCH functionality was introduced in SQL server 2011. Right now we do not have possibility to generate different SQL syntax for different versions of DB. So I am really sorry but this will not be fixed in any reasonable time. Maybe in 0.4.0 we will introduce some way how to support more DB versions in cases like this.

2reactions
class-undefinedcommented, Apr 15, 2022

2022 same mistake

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to catch an error when using mongoose to query ...
Mongoose's findOne() return null when there is no document found and null is not an error. You can use .orFail() which throw an...
Read more >
Mongoose | findOne() Function
The findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns ...
Read more >
mongoose-findone-or-error
Simple Mongoose plugin for rejecting findOne and findById promises which resolve null.
Read more >
db.collection.findOne() — MongoDB Manual
Returns one document that satisfies the specified query criteria on the collection or view. If multiple documents satisfy the query, this method returns...
Read more >
Mongoose v6.8.1: API docs
path «String» The path a.b.c in the doc where this cast error occurred ... Do not use this to create a new Decimal128...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found