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/@sveltejs+kit@2.7.2_@sveltejs+vite-plugin-svelte@4.0.0-next.8_svelte@5.0.0-next.269_vite@5.4._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:51:16)
at async resolve (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@sveltejs+kit@2.7.2_@sveltejs+vite-plugin-svelte@4.0.0-next.8_svelte@5.0.0-next.269_vite@5.4._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/respond.js:454:18)
at async Module.respond (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@sveltejs+kit@2.7.2_@sveltejs+vite-plugin-svelte@4.0.0-next.8_svelte@5.0.0-next.269_vite@5.4._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/respond.js:327:20)
at async file:///Users/**********/WebDev/*****-********/node_modules/.pnpm/@sveltejs+kit@2.7.2_@sveltejs+vite-plugin-svelte@4.0.0-next.8_svelte@5.0.0-next.269_vite@5.4._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/@sveltejs+kit@2.7.2_@sveltejs+vite-plugin-svelte@4.0.0-next.8_svelte@5.0.0-next.269_vite@5.4._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/page/actions.js:51:16)
at async resolve (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@sveltejs+kit@2.7.2_@sveltejs+vite-plugin-svelte@4.0.0-next.8_svelte@5.0.0-next.269_vite@5.4._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/respond.js:454:18)
at async Module.respond (/Users/**********/WebDev/*****-********/node_modules/.pnpm/@sveltejs+kit@2.7.2_@sveltejs+vite-plugin-svelte@4.0.0-next.8_svelte@5.0.0-next.269_vite@5.4._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/runtime/server/respond.js:327:20)
at async file:///Users/**********/WebDev/*****-********/node_modules/.pnpm/@sveltejs+kit@2.7.2_@sveltejs+vite-plugin-svelte@4.0.0-next.8_svelte@5.0.0-next.269_vite@5.4._a77vxbboh36tqqtsa3niclnpnu/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22
4 Replies
siim
siim13mo 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
bobmusinexOP13mo 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
siim13mo 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
bobmusinexOP13mo 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...

Did you find this page helpful?