Cannot read properties of undefined (reading '0')

What is the expected behavior when using get() when there would be no matching results for a given select query? Does it return null or would this be an error? At present it's giving an error. I'm new to discord, but I guess I expected it to return a null. For example, the following query works and returns an empty array:
db.select().from(users).where(eq(users.email, email)).all();
db.select().from(users).where(eq(users.email, email)).all();
whereas changing all() to get() results in error trying to access first element of an empty array (drizzle-orm/utils.js:40:37):
db.select().from(users).where(eq(users.email, email)).get();
db.select().from(users).where(eq(users.email, email)).get();
4 Replies
Dan
Dan•2y ago
Do you know what is the behavior of the raw driver in this case? We tried to model the execution methods in SQLite the same way they work in raw drivers
tacomanator
tacomanatorOP•2y ago
I'm using better-sqlite3. Assuming the equivalent statement is something like:
db.prepare("SELECT * FROM users WHERE email = ?").get(email);
db.prepare("SELECT * FROM users WHERE email = ?").get(email);
If there are no matching records, there is no error and the result will be undefined. The error happens at line 34 of utils.ts: https://github.com/drizzle-team/drizzle-orm/blob/70ff50bb7c9bd956bae8cceca937374614576c11/drizzle-orm/src/utils.ts#L34 The driver is returning undefined for the row value (as expected), but the mapResultRow is not checking for that, and attempts the process the values anyway. Before logging a bug I thought I had better check if this is expected or not.
GitHub
drizzle-orm/utils.ts at 70ff50bb7c9bd956bae8cceca937374614576c11 · ...
TypeScript ORM for SQL. Contribute to drizzle-team/drizzle-orm development by creating an account on GitHub.
tacomanator
tacomanatorOP•2y ago
GitHub
[BUG]: Cannot read properties of undefined (reading '0') on get() w...
What version of drizzle-orm are you using? 0.23.2 Describe the Bug Assuming a users table... const users = sqliteTable( "users", { email: text("email").notNull() }, // ... (user...
Dan
Dan•2y ago
Thanks! We'll definitely check and fix once we figure out our current stuff 🫠
Want results from more Discord servers?
Add your server