I was not using 'use client'
at the top of my function that was calling the database. I thought that Next.js automatically loads pages as server components so I assumed it didn't need the clarification. Kind of a stupid but common mistake so I hope anyone who has the same issue checks that every function they use their environment variables in should be marked 'use client'
.