node-sqlite3 icon indicating copy to clipboard operation
node-sqlite3 copied to clipboard

Passing 'undefined' on failure to select.

Open averykimball opened this issue 10 years ago • 6 comments

When a query finds no record, it passes 'undefined' to the callback (and sets no error). Is this the correct behavior?

I'm certain users of your library are now used to this behavior, and changing it would be a huge (breaking) hassle, but having to handle undeclared values being passed around is... awkward. Doing a typeof check (as opposed to a 'null' check) for returned records doesn't seem like the appropriate way to do things.

Since I checked all the issues, and no-one else is complaining, I'm open to the idea that I'm very wrong and I'm using your library incorrectly.

averykimball avatar Oct 03 '16 10:10 averykimball

Is the suggestion to set an error, or to pass a different kind of value? undefined seems to be to be the correct behavior, since "not finding data" is not an error state. Malformed queries, broken databases, and so on are errors, whereas lack of data is a valid value.

tmcw avatar Oct 03 '16 16:10 tmcw

what about an empty array or empty object? [] or {}

kroggen avatar Oct 03 '16 18:10 kroggen

It's much harder to detect an empty object than it is to detect undefined or null - the latter are falsy, the former is not. I think the current behavior is correct.

tmcw avatar Oct 03 '16 18:10 tmcw

Closing; current behavior is correct and matches other libraries.

tmcw avatar Oct 03 '16 18:10 tmcw

Thanks for taking the time to address this. My suggestion would be a null, as I am using passport, and passport deauthorizes when null is passed to it's deserializer (probably because MongoDB returns null when no document satisfies a query, and passport is very nosql friendly).

Ultimately, adding one typeof check to my code will be easier than changing your and everyone's code base. Cheers!

averykimball avatar Oct 04 '16 09:10 averykimball

It also make async lib to not work how expected, better will be null.

mihailssh avatar Mar 20 '17 22:03 mihailssh