Comments (4)
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.
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.
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.
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)
- json1 extension HOT 1
- Best way to batch create/insert or update? (How to properly use driver.serialize?) HOT 1
- Unable to open DB read only HOT 9
- Can't execute transaction HOT 3
- placeholders don't work with pragma HOT 2
- SQLCipher support HOT 2
- Problem with nullable fields in knex HOT 1
- Placeholders don't work with exec
- Exceptions don't always contain stack traces HOT 3
- electron-forge make error HOT 1
- Access sqlite database which has password HOT 1
- migration logic HOT 2
- impossible to change migration configuration HOT 2
- sqlite api to create a db HOT 4
- Date and time in db.run HOT 3
- Getting multiple errors in React TypeScript HOT 1
- bun.sh driver? HOT 2
- Why are sqlite3 typings included here when sqlite3 has its own typings? HOT 9
- Unable to install Sqlite HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-sqlite.