Type Error when inserting into table

I'm getting as error when trying to insert into a table using:
await db.insert(users).values({
id: userId,
email: email,
passwordHash: passwordHash
});
await db.insert(users).values({
id: userId,
email: email,
passwordHash: passwordHash
});
I can run the insert successfully in Drizzle Runner. The error:
TypeError: Cannot read properties of undefined (reading 'insert')
at default (/Users/**********/WebDev/*****-********/src/routes/signup/+page.server.ts:45:12)
at async Module.handle_action_json_request (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:51:16)
at async resolve (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/respond.js:454:18)
at async Module.respond (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/respond.js:327:20)
at async file:///Users/**********/WebDev/*****-********/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22
TypeError: Cannot read properties of undefined (reading 'insert')
at default (/Users/**********/WebDev/*****-********/src/routes/signup/+page.server.ts:45:12)
at async Module.handle_action_json_request (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:51:16)
at async resolve (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/respond.js:454:18)
at async Module.respond (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/respond.js:327:20)
at async file:///Users/**********/WebDev/*****-********/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22
4 Replies
siim
siim3mo ago
Probably going to need more information for this, the type error isn't that informative. How are you exporting/importing db into your file? I'd start there
bobmusinex
bobmusinexOP3mo ago
export const db = drizzle('./src/lib/db/sqlite.db'); import db from '$lib/db/db'; For context, $lib is a built-in SvelteKit alias. It works elsewhere in my project. I'm able to successfully query data.
siim
siim3mo ago
I'm not too familiar with SvelteKit so maybe someone else can speak more to that, but there is some kind of issue happening with the way db is imported for sure (just based on the error message). It might be something to do with the way SvelteKit uses that alias (maybe an issue with server/client boundaries?) but like I said I'm not familiar with SvelteKit
bobmusinex
bobmusinexOP3mo ago
Ok, fixed it. The alias wasn't the probably, it's just rendered to a plaintext filepath. The issue was import db should have been import { db }. Always the little things...
Want results from more Discord servers?
Add your server