Cannot read properties of undefined (reading 'findMany')

Hello, I have a simple schema and an empty table and I can call my database wiith sqllike query but not with the querybuilder, i dont understand why, am I missing something? ty guys
const accounts = pgTable('accounts', {
id: serial('id').primaryKey(),
});

module.exports = { accounts }
const accounts = pgTable('accounts', {
id: serial('id').primaryKey(),
});

module.exports = { accounts }
const con = drizzle('postgres://postgres:root@localhost:5432/eurotruck')
const { accounts }= require('../../../db/schema')
const manies = await con.select().from(accounts)
// => []
const manies2 = await con.query.accounts.findMany()
// = Cannot read properties of undefined (reading 'findMany')
const con = drizzle('postgres://postgres:root@localhost:5432/eurotruck')
const { accounts }= require('../../../db/schema')
const manies = await con.select().from(accounts)
// => []
const manies2 = await con.query.accounts.findMany()
// = Cannot read properties of undefined (reading 'findMany')
18 Replies
TOSL
TOSL3w ago
What you shared here is written const accounts it needs to be export const accounts if it is not that is likely the error. If not, let me know. Oh and I'm talking about your schema. You need to export everything in your schema Then you can import it
suljii
suljiiOP3w ago
Oh yeah mb didnt put the exports in my example Its not the error tho
TOSL
TOSL3w ago
Is
Cannot read properties of undefined (reading 'accounts')
Cannot read properties of undefined (reading 'accounts')
the whole error?
suljii
suljiiOP3w ago
[ script:queue] TypeError: Cannot read properties of undefined (reading 'findMany')
[ script:queue] at @queue/server.js:27:50
[ script:queue] at processTicksAndRejections (node:internal/process/task_queues:96:5)
[ script:queue] at async citizen:/scripting/v8/main.js:482:9
[ script:queue] TypeError: Cannot read properties of undefined (reading 'findMany')
[ script:queue] at @queue/server.js:27:50
[ script:queue] at processTicksAndRejections (node:internal/process/task_queues:96:5)
[ script:queue] at async citizen:/scripting/v8/main.js:482:9
Tenkes
Tenkes3w ago
module.exports { accounts }
module.exports { accounts }
should be
module.exports = { accounts }
module.exports = { accounts }
suljii
suljiiOP3w ago
it is sorry, its my bad i corrected it with my phone the problem is : that line is working const manies = await con.select().from(accounts) but that line not working const manies2 = await con.query.accounts.findMany() i dont understand how its possible i have to be missing soemthing
Tenkes
Tenkes3w ago
Oh I think you need to define schema in your drizzle connection. Here's code snipped from my project:
import { Pool, neonConfig } from '@neondatabase/serverless'
import { drizzle } from 'drizzle-orm/neon-serverless'
import * as schema from './schema' // <- importing schema

const pool = new Pool({ connectionString: process.env.DATABASE_URL })

export const db = drizzle({ client: pool, schema }) // passing schema to drizzle connection
import { Pool, neonConfig } from '@neondatabase/serverless'
import { drizzle } from 'drizzle-orm/neon-serverless'
import * as schema from './schema' // <- importing schema

const pool = new Pool({ connectionString: process.env.DATABASE_URL })

export const db = drizzle({ client: pool, schema }) // passing schema to drizzle connection
I believe this is required if you want to use db.query
suljii
suljiiOP3w ago
can can you show me how you call it? like a findMany
Tenkes
Tenkes3w ago
same like you did
TOSL
TOSL3w ago
He is talking about passing your schema to drizzle()
suljii
suljiiOP3w ago
yes ive just done it
TOSL
TOSL3w ago
Your findMany is written correctly. Does the query work now?
suljii
suljiiOP3w ago
const { drizzle } = require('drizzle-orm/node-postgres')
const schemas = require('../../../db/schema')
const { Pool } = require('pg')

const pool = new Pool({
connectionString: 'postgres://postgres:root@localhost:5432/eurotruck'
})
console.log(schemas)

const db = drizzle({ client: pool, schemas })

module.exports = { db }
const { drizzle } = require('drizzle-orm/node-postgres')
const schemas = require('../../../db/schema')
const { Pool } = require('pg')

const pool = new Pool({
connectionString: 'postgres://postgres:root@localhost:5432/eurotruck'
})
console.log(schemas)

const db = drizzle({ client: pool, schemas })

module.exports = { db }
same error nah, same error ok new error its now findMany() who is undefined again, so he knows my schema
TOSL
TOSL3w ago
You shouldn't need to create a pool. just pass you sql connection directly as the first parameter then pass schema as an object
const db =drizzle(database_url, {schema})
const db =drizzle(database_url, {schema})
Also just curious why are you not just using import/export I can't think of reason for your node version to not be 16+ should have support for es modules
suljii
suljiiOP3w ago
const db =drizzle(database_url, {schema})
const db =drizzle(database_url, {schema})
in that case schema is my unique accounts schema or the object that countain all my schema like {accounts, schema2 ... } ? because i cant use "type":"module" in my package.json dunno why
TOSL
TOSL3w ago
check your node version preferably you should be in 16 at least
suljii
suljiiOP3w ago
its 18
Tenkes
Tenkes3w ago
it can also be
const { accounts } = require('../../../db/schema')

const db = drizzle(database_url, {
schema: {
accounts
}
})
const { accounts } = require('../../../db/schema')

const db = drizzle(database_url, {
schema: {
accounts
}
})
but it's simpler to import everything from your schema file and name it schema, so you can pass that to your drizzle(). just like I did in code snippet I sent from my project and then whenever you define new tables to that file it will already be passed to drizzle(), since it's taking everything from that file

Did you find this page helpful?