Coder Social home page Coder Social logo

Comments (4)

theogravity avatar theogravity commented on May 29, 2024

I looked into this and there's actually two kinds of errors that can be thrown.

  • At the function call level. This could be SQL parse or connection errors. This follows the [complete] parameter
  • At the callback level. It's not clear what kind of errors are thrown at the callback level. This follows the [callback] parameter

The implementation properly follows the documented API:

https://github.com/TryGhost/node-sqlite3/wiki/API#databaseeachsql-param--callback-complete

I have updated the readme to clarify this further.

from node-sqlite.

NabiKAZ avatar NabiKAZ commented on May 29, 2024

You said the implementation follows the original documentation.
But in this example you can see that exactly the same code calls the callback by the original project (db) but by this project (db2) an exception error is generated.

import sqlite3 from 'sqlite3';
var db = new sqlite3.Database('test.db');

import { open } from 'sqlite'
const db2 = await open({
  filename: 'test.db',
  driver: sqlite3.Database
});

db -> sqlite3 [original]

await db.each(
	'SELECT * FROM test WHERE score1 = ?', [1001],
	(err, row) => {
		console.log(">>> ERR:", err);
		console.log("row", row);
	}
);

result:

>>> ERR: [Error: SQLITE_ERROR: no such column: score1] {
  errno: 1,
  code: 'SQLITE_ERROR'
}
row undefined

db2 -> sqlite [this]

await db2.each(
	'SELECT * FROM test WHERE score1 = ?', [1001],
	(err, row) => {
		console.log(">>> ERR:", err);
		console.log("row", row);
	}
);

result:

node:internal/process/esm_loader:94
    internalBinding('errors').triggerUncaughtException(
                              ^

[Error: SQLITE_ERROR: no such column: score1] {
  errno: 1,
  code: 'SQLITE_ERROR'
}

Node.js v17.2.0

from node-sqlite.

theogravity avatar theogravity commented on May 29, 2024

Here's the implementation:

      this.db.each(
        sqlObj.sql,
        ...sqlObj.params,
         // [callback]
        (err, row) => {
          if (err) {
            return callback(formatError(err), null)
          }

          callback(null, row)
        },
        // [complete]
        (err, count) => {
          if (err) {
            return reject(formatError(err))
          }

          resolve(count)
        }
      )

If there is an error in the rows [callback], it's not going to reject the promise, but instead call the callback with the error populated. Your situation happens because it's a direct error (there are no rows to also return), so [callback] won't ever get called and so the error is thrown in the [complete] callback.

In your db -> sqlite3 [original] example, you have:

await db.each(
	'SELECT * FROM test WHERE score1 = ?', [1001],
	(err, row) => {
		console.log(">>> ERR:", err);
		console.log("row", row);
	}
);
  • The native db.each() call is not a promise
  • You're missing the completion callback, which is optional, so according to documentation, it looks like it combines the error into the row result

The core difference is sqlite is implementing the [complete] callback so it knows that the promise is to be resolved / rejected. Without it, it would not know when the call has finished returning rows and would make having .each() be a promise pointless.

from node-sqlite.

NabiKAZ avatar NabiKAZ commented on May 29, 2024

I did not understand exactly what happened! But it does not matter! (:
My only goal was to see the difference in functionality between your version and the original version, which you seem to understand, and which seems normal.
Thanks.

from node-sqlite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.