drizzle-team / drizzle-team/drizzle-orm

[BUG] [MySQL2]: Undefined/nullish problem passing `drizzle` dao through functions as argument

Open
#1,843 0 comments 0 reactions 0 assignees View on GitHub
bug qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

^0.29.3

### What version of `drizzle-kit` are you using?

^0.20.13

### Describe the Bug

## Problem

I've been trying some different approaches to use the drizzle dao object, this time I tried to pass the object to a method called `set[n]Routes` that passes the object to the actual fastify routes. Last time I used Drizzle I was creating a registration class that received the dao object through dependency injection. This time it is not working and I am receiving the following error message:

`Unexpected token 'C', \"Cannot rea\"... is not valid JSON`

This complete message says that is not possible to read the property 'map' from undefined. I could not recover more details than that and I am going to use the way I used to use drizzle before. IMPORTANT: I also checked if 'db' as null, but wasn't, it was a valid drizzle object.

I tried to call the dao object directly after creating it and it worked without problems:

```ts
const db: Database = drizzle(queryClient);

console.log('test stuff')
const test = await db.select().from(userTable);
console.log(test);
console.log('test end')
```
![image](https://github.com/drizzle-team/drizzle-orm/assets/70824102/1e310118-d324-4021-a832-1ae2e5cccbff)

That's how I am trying to call the functions to register the fastify routes:

```ts
// main.ts
...
const db: Database = drizzle(queryClient);

console.log('test stuff')
const test = await db.select().from(userTable);
console.log(test);
console.log('test end')

const corsOptions: FastifyCorsOptions = {
origin: process.env.CURRENT_ENV === 'development'
? true
: process.env.BACKEND_HOST
};

fastify.register(cors, corsOptions);

setAuthRoutes(fastify, db);
...
```
```ts
//routes.ts
function setAuthRoutes(fastify: FastifyInstance, db: Database) {
fastify.post('/login', async (req, res) => {
try {
const request = await LoginRequest.parseAsync(req.body);
const result = await login(request.login, request.password, db);
await res.send(result);
}
...
```
```ts
//login.ts (where the exception is being raised)
...
const login = async (login: string, password: string, db: Database) => {
const match = (await db
.select()
.from(userTable)
.where(or(
eq(userTable.email, login),
eq(userTable.username, login)
)))[0];
...
```

### Expected behavior

The data was supposed to be got correctly.

### Environment & setup

OS: Windows
DB: It's coming from a docker container with all the migrations set up (not from Drizzle, they came from another program, this one is read-only)
Compiler: SWC + TypeScript

This is my tsconfig:

```json
{
"compilerOptions": {
"module": "NodeNext",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "ESNext",
"noImplicitAny": true,
"moduleResolution": "NodeNext",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.