DT
Drizzle Team•4w ago
Tony

Error: undefined is not an object

Getting the following error:
84 | span?.setAttributes({
85 | "drizzle.query.name": query.name,
86 | "drizzle.query.text": query.text,
87 | "drizzle.query.params": JSON.stringify(params)
88 | });
89 | return client.query(query, params);
^
TypeError: undefined is not an object (evaluating 'client.query')
at C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\node-postgres\session.js:89:16
at C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\node-postgres\session.js:83:35
at C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\node-postgres\session.js:69:54
at execute (C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\node-postgres\session.js:68:17)
at then (C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\query-promise.js:21:17)
84 | span?.setAttributes({
85 | "drizzle.query.name": query.name,
86 | "drizzle.query.text": query.text,
87 | "drizzle.query.params": JSON.stringify(params)
88 | });
89 | return client.query(query, params);
^
TypeError: undefined is not an object (evaluating 'client.query')
at C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\node-postgres\session.js:89:16
at C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\node-postgres\session.js:83:35
at C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\node-postgres\session.js:69:54
at execute (C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\node-postgres\session.js:68:17)
at then (C:\Users\ThatTonybo\Desktop\deepslate\api\node_modules\drizzle-orm\query-promise.js:21:17)
Seems to be occuring at the following code in my app:
// const db = drizzle(process.env.DATABASE_URL as string);

const [account] = await db
.select({ id: accountsTable.id, passwordHash: accountsTable.passwordHash })
.from(accountsTable)
.where(eq(lower(accountsTable.email), email.toLowerCase()));
// const db = drizzle(process.env.DATABASE_URL as string);

const [account] = await db
.select({ id: accountsTable.id, passwordHash: accountsTable.passwordHash })
.from(accountsTable)
.where(eq(lower(accountsTable.email), email.toLowerCase()));
Any idea what's occuring here? Doesn't happen with any other database calls including similar calls with the same .where arguments
35 Replies
Tony
TonyOP•4w ago
I've managed to track it down, it seems to be occuring in my insert statement here:
await db.insert(sessionsTable).values(session);
await db.insert(sessionsTable).values(session);
I'm still really unsure why this is happening, none of my other inserts fail. This is the schema also:
export const sessionsTable = pgTable('sessions', {
id: text('session_id').notNull(),
accountId: cuid2('account_id')
.notNull()
.references(() => accountsTable.id),
createdAt: timestamp('created_at').notNull(),
expiresAt: timestamp('expires_at').notNull()
});
export const sessionsTable = pgTable('sessions', {
id: text('session_id').notNull(),
accountId: cuid2('account_id')
.notNull()
.references(() => accountsTable.id),
createdAt: timestamp('created_at').notNull(),
expiresAt: timestamp('expires_at').notNull()
});
TOSL
TOSL•4w ago
Other queries work?
Tony
TonyOP•4w ago
Yeah everything else works. I have inserts working on other tables. It's only the sessions table I've confirmed it's the insert too, not the query, that seems to work fine
TOSL
TOSL•4w ago
And you did push/migrate the sessions table?
Tony
TonyOP•4w ago
Yep, all up to date on migrate What's confusing too is that this same code worked on a previous project, I just posted it over and it's giving this error now
TOSL
TOSL•4w ago
That's very interesting. Are the Drizzle versions the same between projects? orm and kit
Tony
TonyOP•4w ago
Original working one is 0.38.2 on orm, 0.30.1 on kit. Current not working one is 0.38.3 on orm and 0.30.1 on kit
TOSL
TOSL•4w ago
Can you try doing a test insert on that table in drizzle studio?
Tony
TonyOP•4w ago
Don't think I actually have drizzle studio, I'll look into it
TOSL
TOSL•4w ago
include in kit npm drizzle-kit studio you will need to specify you db connection credentials in drizzle.config.ts
Tony
TonyOP•4w ago
Alright, I'm out at the moment but I'll give it a shot as soon as I'm back
Tony
TonyOP•4w ago
I tried studio, and unless I'm not doing the insert right, getting this
No description
Tony
TonyOP•4w ago
Setting the session_id as primary key in my schema lets me insert
No description
Tony
TonyOP•4w ago
However the actual error still occurs in the ORM
TOSL
TOSL•3w ago
Oh duh I can't believe I didn't notice you were missing a PK on the table. I blame it being late. So you did this?
export const sessionsTable = pgTable('sessions', {
id: text('session_id').primaryKey().notNull(),
accountId: cuid2('account_id')
.notNull()
.references(() => accountsTable.id),
createdAt: timestamp('created_at').notNull(),
expiresAt: timestamp('expires_at').notNull()
});
export const sessionsTable = pgTable('sessions', {
id: text('session_id').primaryKey().notNull(),
accountId: cuid2('account_id')
.notNull()
.references(() => accountsTable.id),
createdAt: timestamp('created_at').notNull(),
expiresAt: timestamp('expires_at').notNull()
});
And it's the exact same error?
Tony
TonyOP•3w ago
Yeah haha. But yep, I did that, and same issue Studio lets me insert but same error from the orm It's strange cause my previous project's code worked even without the primary key, i'm very confused about it
TOSL
TOSL•3w ago
And you're sure it's the insert that's throwing the error?
Tony
TonyOP•3w ago
Yep, that's exactly what it's coming from. Commenting it out and everything works fine, i've got it in a try catch and it's absolutely where the error is coming from
TOSL
TOSL•3w ago
can you change cuid2 for accountId to just be a varchar/text column? on the sessionTable
Tony
TonyOP•3w ago
Yeah I could, and just remove the account reference maybe?
TOSL
TOSL•3w ago
You'd still need the reference for your session. I just curious is the column type of cuid2 is giving you trouble.
Tony
TonyOP•3w ago
Hm, changed the type from cuid2 to text and ran push, no changes?
No description
TOSL
TOSL•3w ago
And the insert?
Tony
TonyOP•3w ago
Same error 🙃
TOSL
TOSL•3w ago
what values are you inserting?
Tony
TonyOP•3w ago
Well this is the insert code
const session: typeof sessionsTable.$inferInsert = {
id,
accountId,
createdAt: new Date(),
expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30)
};

try {
await db.insert(sessionsTable).values(session);
} catch(err) {
console.error(err);
}
const session: typeof sessionsTable.$inferInsert = {
id,
accountId,
createdAt: new Date(),
expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30)
};

try {
await db.insert(sessionsTable).values(session);
} catch(err) {
console.error(err);
}
id is a string, made from encoding the session token which is some crypto stuff accountId being the cuid2 string that's on the account (the test account i'm using has an ID of ss7f96cyakd6dy6k9y1idbhp for example) And createdAt/expiresAt are self explanatory
TOSL
TOSL•3w ago
Can you enable logging on you drizzle initialization? const db = drizzle({ logger: true });
Tony
TonyOP•3w ago
Yup I can I do const db = drizzle(...) in every file I use the db, do I have to throw logger on all of them or just the one in the index file?
TOSL
TOSL•3w ago
Is there some specific reason you aren't using just one global initialization?
Tony
TonyOP•3w ago
Not really, might quickly change that
TOSL
TOSL•3w ago
I would recommend that. Bound to cause issues in the future if it isn't already
Tony
TonyOP•3w ago
Yeah, uhh... it seems using one global one fixed it, insert just worked fine Guess that does make sense, lol. Strange it caused no problems on my other project but I'll keep that in mind for the future
TOSL
TOSL•3w ago
Perfect. Glad that simple change fix it. I'm sure there are a dozen little edge cases where a non global db client will or won't work. But in the long run you can always bet on it causes a problem
Tony
TonyOP•3w ago
Yeah, pretty clear there haha. On the upside, having a reliable server error for so long let me perfect my front end's error handling so I'll take that I appreciate your help on it also
TOSL
TOSL•3w ago
You're welcome

Did you find this page helpful?