W
Wasp-lang•2mo ago
mm

Uncaught Error when using prisma inside `app/src/server/utils.ts`

While working on creating jobs, I created a utility for logging inside the server/utils.ts I wasn't checking the front-end from time to time as I was testing the job using postman. However upon visiting the front-end @ localhost it doesn't work, it says uncaught error. This was hard to debug as the terminal says wasp is building correctly, it's basically a runtime error. Had to check the which commit this started and check the code changes. At first I thought I did something wrong in the jobs. Is this an expected behavior? * I attached the browser console error Here is the code:
import { prisma } from "wasp/server";

export function requireNodeEnvVar(name: string): string {
const value = process.env[name];
if (value === undefined) {
throw new Error(`Env var ${name} is undefined`);
} else {
return value;
}
}

interface LogParams {
message: string;
level: 'info' | 'error' | 'job-info' | 'job-error' | string;
data?: Record<string, any>;
}

export const logger = async ({ message, level = 'info', data = {} }: LogParams) => {
await prisma.logs.create({
data: {
message: message,
level: level,
data: data,
},
});
}
import { prisma } from "wasp/server";

export function requireNodeEnvVar(name: string): string {
const value = process.env[name];
if (value === undefined) {
throw new Error(`Env var ${name} is undefined`);
} else {
return value;
}
}

interface LogParams {
message: string;
level: 'info' | 'error' | 'job-info' | 'job-error' | string;
data?: Record<string, any>;
}

export const logger = async ({ message, level = 'info', data = {} }: LogParams) => {
await prisma.logs.create({
data: {
message: message,
level: level,
data: data,
},
});
}
No description
17 Replies
kapa.ai
kapa.ai•2mo ago
Hi @mm. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the provided information, it seems like you're trying to use Prisma in a utility function for logging. However, there might be an issue with how Prisma is being initialized or used. One of the common issues with Prisma is that it might not be initialized correctly. For example, in this issue (https://github.com/wasp-lang/wasp/issues/427), the user had to manually import and initialize PrismaClient to make it work.
import { PrismaClient } from './node_modules/.prisma/client'
const prisma = new PrismaClient()
import { PrismaClient } from './node_modules/.prisma/client'
const prisma = new PrismaClient()
Another common issue is when there are no entities defined in the main.wasp file, the server might crash with a Prisma error as described in this issue (https://github.com/wasp-lang/wasp/issues/398). Also, there might be an issue with the environment variable DATABASE_URL not being found as described in this issue (https://github.com/wasp-lang/wasp/issues/1321) and this issue (https://github.com/wasp-lang/wasp/issues/1282). Please make sure that Prisma is correctly initialized and the DATABASE_URL environment variable is correctly set. If the problem persists, it might be a good idea to open an issue on the Wasp GitHub repository. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @mm, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho•2mo ago
Kapa is not really helpful here 😄 Are you importing this logger on the client? Or are you importing some file on the client that imports the logger? I see the errors originates from the config.ts file. Importing server code on the client is not really possible 🙂 e.g. the process variable exists only in Node.js
mm
mm•2mo ago
No I'm only using the logger on the server side Yes it says config.ts you mean if it's an issue with config.ts high probability that it's being used on client side? So there is a probability that this utility is being considered as client code? what do you think that's why I can't put back-end specific code in it? what I did to make it work is to simply pass the PrismaClient
export const logger = async (prisma: PrismaClient, { message, level = 'info', data = {} }: LogParams) => {
await prisma.logs.create({
data: {
message: message,
level: level,
data: data,
},
});
}
export const logger = async (prisma: PrismaClient, { message, level = 'info', data = {} }: LogParams) => {
await prisma.logs.create({
data: {
message: message,
level: level,
data: data,
},
});
}
I'm using it inside a job
miho
miho•2mo ago
Simply put, you shouldn't import server/utils.ts in your client code like React components. I'm not sure if you are importing it directly in some React component or you are importing it in the config.ts file and then import importing the config.ts in some React component. Please check all the import Something from Something bits in your client code to make sure it's not happening.
mm
mm•2mo ago
I'm not using it directly inside a client code I only used it for jobs in this case
miho
miho•2mo ago
And do you have a config.ts file?
mm
mm•2mo ago
nope let me double check
mm
mm•2mo ago
No description
mm
mm•2mo ago
these are the files that make uses the utils
miho
miho•2mo ago
Where does the browser take you when you click on the filename in the error message? config.ts:41:20 what do you see?
mm
mm•2mo ago
I'll check later after deployment. or can I run wasp start while deploying??
mm
mm•2mo ago
Here's the file
No description
MEE6
MEE6•2mo ago
Wohooo @mm, you just became a Waspeteer level 4!
mm
mm•2mo ago
When I have the time I will try this out in a fresh opensaas setup. I kinda re-organized the directories for this project maybe that's causing this? though I'm not sure if it's supposed to be an issue.
miho
miho•2mo ago
You are importing wasp/server/config on the client?
Want results from more Discord servers?
Add your server