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
bloberenober
bloberenober15mo 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
tacomanator15mo 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
tacomanator15mo 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...
bloberenober
bloberenober15mo ago
Thanks! We'll definitely check and fix once we figure out our current stuff 🫠
Want results from more Discord servers?
Add your server
More Posts
Is it recommended to create a SQL transaction every time we try to create a foreign relationship?prisma used to wrap every foreign upsert (2 entities) within a transaction to guarantee that both roDoesn't drizzle-kit generate:pg work yet when schema imports from esm package?``` > drizzle-kit generate:pg --out src/db/migrations --schema src/db/schemas drizzle-kit: v0.17.0 Is there `.returning()` in insert statement in MySQL like SQLite?There is `.returning()` insert statement in SQLite but MySQL not. how do I `.returning()` in insertCan you please release this commit?https://github.com/drizzle-team/drizzle-orm/commit/63134f21ab5ccbdb651a63c1ac0f6d5e8bbe2002 I'm getGetting results in document form rather than recordHi, new to drizzle but liking it so far. Is there away to get the results in more of a 'document' strequire() of ES Module is not supported planetscale serverless + sveltekitHi, I am getting this error when I user planetscale serverless with drizzle orm ``` require() of ESMySQL Standalone queryBuilder?https://discord.com/channels/1043890932593987624/1052239146699210863/1085708651374837810 Where shouWhat is the type for an .orderBy() parameterInside of a function, i have a query more complicated than this, but something like: ```ts db.selecMySqlInsertValue<> vs typeof Table type mismatchI've got this line of code: ```ts await db.insert(table).values(props); ``` Where `props` is showiArgument of type 'SQL<unknown>' is not assignable to parameterI have the following: ```ts const projects = await db .select() .from(project) .where