TypeError: Cannot read properties of undefined (reading 'findMany')
at Module.getAllRestaurantsByVendor (projectfolder/src/modules/restaurant/db.ts:22:74)
at Module.GET (projectfolder/src/pages/api/restaurant/index.ts:17:48)
at eval (projectfolder/src/pages/restaurant/index.astro:27:46)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async callComponentAsTemplateResultOrResponse (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/runtime/server/render/astro/render.js:87:25)
at async renderToAsyncIterable (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/runtime/server/render/astro/render.js:115:26)
at async renderPage (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/runtime/server/render/page.js:31:24)
at async Response.status (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/core/render-context.js:80:25)
at async callMiddleware (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/core/middleware/callMiddleware.js:11:10)
at async RenderContext.render (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/core/render-context.js:96:22)
Error: Error in getAllRestaurantsByVendor: TypeError: Cannot read properties of undefined (reading 'findMany')
at Module.getAllRestaurantsByVendor (projectfolder/src/modules/restaurant/db.ts:33:11)
at Module.GET (projectfolder/src/pages/api/restaurant/index.ts:17:48)
at eval (projectfolder/src/pages/restaurant/index.astro:27:46)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async callComponentAsTemplateResultOrResponse (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/runtime/server/render/astro/render.js:87:25)
at async renderToAsyncIterable (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/runtime/server/render/astro/render.js:115:26)
at async renderPage (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/runtime/server/render/page.js:31:24)
at async Response.status (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/core/render-context.js:80:25)
at async callMiddleware (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/core/middleware/callMiddleware.js:11:10)
at async RenderContext.render (file:///projectfolder/node_modules/.pnpm/astro@4.4.15_typescript@5.4.2/node_modules/astro/dist/core/render-context.js:96:22)
I'm trying to use findMany in @modules/restaurant/db.ts which is used in a GET request at /api/restaurant
import db from "@lib/db";
try {
const result = await db.query.Restaurants.findMany({
where: eq(restaurants.vendor_id, vendor_id),
columns: {
id: true,
}
})
return result;
} catch (error) {
throw new Error("Error in getAllRestaurantsByVendor: " + error);
}
import { users, vendors, restaurants } from "@/lib/schema";
import { relations } from "drizzle-orm";
export const usersRelations = relations(users, ({ one, many }) => ({
// a user can be a vendor
vendor: one(vendors, {
fields: [users.id],
references: [vendors.user_id],
}),
}));
export const vendorsRelations = relations(vendors, ({ one, many }) => ({
// a vendor is a user
user: one(users, {
fields: [vendors.user_id],
references: [users.id],
}),
// a vendor can have multiple restaurants
restaurants: many(restaurants),
}));
export const restaurantsRelations = relations(restaurants, ({ one, many }) => ({
// a restaurant is owned by a vendor
vendor: one(vendors, {
fields: [restaurants.vendor_id],
references: [vendors.id],
}),
}));
this is my db.ts at @lib/db.ts
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import type { MyDatabase } from "@/lib/schema";
import * as schema from "@/lib/schema";
export const connectionString = process.env.SECRET_SUPABASE_URL || import.meta.env.SECRET_SUPABASE_URL;
if (!connectionString) {
throw new Error("DATABASE_URL is missing");
}
declare global {
var db: MyDatabase | undefined;
}
let db: MyDatabase;
if (!global.db) {
// Disable prefetch as it is not supported for "Transaction" pool mode
const client = postgres(connectionString, { prepare: false });
global.db = drizzle(client, { logger: true, schema });
}
db = global.db;
export default db;
Expected behavior
Even though I can just use db.select to achieve what I wanted, but I wonder if I'm using the findMany correctly to achieve the same thing (get all the restaurant ids of that vendor) and is my setup correct? Also, I'm not getting any autocomplete when inside the with curly braces as I saw on youtube that it's supposed to suggest the table names.
What version of
drizzle-orm
are you using?0.30.0
What version of
drizzle-kit
are you using?^0.20.14
Describe the Bug
I'm using Supabase too. I'm getting this error:
I'm trying to use findMany in @modules/restaurant/db.ts which is used in a GET request at /api/restaurant
this is my schema at @lib/schema.ts
this is my relation.ts at @lib/relation.ts
this is my db.ts at @lib/db.ts
Expected behavior
Even though I can just use db.select to achieve what I wanted, but I wonder if I'm using the findMany correctly to achieve the same thing (get all the restaurant ids of that vendor) and is my setup correct? Also, I'm not getting any autocomplete when inside the
with
curly braces as I saw on youtube that it's supposed to suggest the table names.Environment & setup
No response