title: Error adding New User to database with Next.js | Prisma | Heroku
body: <p>I'm working on my first full-stack app project following along with an online course. I've hit a roadblock that I've been unable to resolve after hours of troubleshooting. The error occurs when attempting to add a new user to a Postgres DB via the command line.</p>
  <pre><code>import bcrypt from 'bcrypt'
  import jwt from 'jsonwebtoken'
  import cookie from 'cookie'
  import { NextApiRequest, NextApiResponse } from 'next'
  import prisma from '../../lib/prisma'
  
  export default async (req: NextApiRequest, res: NextApiResponse) =&gt; {
      const salt = bcrypt.genSaltSync()
      const { email, password } = req.body
  
      let user
  
      try {
          user = await prisma.user.create({
              data: {
              email,
              password: bcrypt.hashSync(password, salt),
          },
        })
      } catch (e) {
        res.status(401)
        res.json({ error: 'User already exists' })
        return
      }
  
      const token = jwt.sign(
      {
        email: user.email,
        id: user.id,
        time: Date.now(),
      },
        'hello',
        { expiresIn: '8h' }
      )
  
    res.setHeader(
      'Set-Cookie',
      cookie.serialize('TRAX_ACCESS_TOKEN', token, {
        httpOnly: true,
        maxAge: 8 * 60 * 60,
        path: '/',
        sameSite: 'lax',
        secure: process.env.NODE_ENV === 'production',
      })
    )
  
    res.json(user)
  }
  </code></pre>
  <p>I am attempting to add the user with the below command via HTTPie:</p>
  <pre><code>http POST :3000/api/signup email=h@h.com password=abcabc
  </code></pre>
  <p>I was getting an error on the data attribute that was resolved after running 'prisma generate'.</p>
  <p>However, I am still getting an error: User already exists.</p>
  <p>I can verify the user does not exist by checking the Prisma Studio in the Users table and there is no new user created.</p>
  <p>Response:</p>
  <pre><code>HTTP/1.1 401 Unauthorized
  Connection: keep-alive
  Content-Length: 31
  Content-Type: application/json; charset=utf-8
  Date: Sun, 05 Jun 2022 19:57:59 GMT
  ETag: &quot;1f-lFUySNKwX3L5eGEwGcNcUoVKWhE&quot;
  Keep-Alive: timeout=5
  Vary: Accept-Encoding
  
  {
      &quot;error&quot;: &quot;User already exists&quot;
  }
  </code></pre>
  <p>This is my schema.prisma file:</p>
  <pre><code>    // This is your Prisma schema file,
  // learn more about it in the docs: https://pris.ly/d/prisma-schema
  
  generator client {
    provider = &quot;prisma-client-js&quot;
  }
  
  datasource db {
    provider          = &quot;postgresql&quot;
    url               = env(&quot;DATABASE_URL&quot;)
    shadowDatabaseUrl = env(&quot;SHADOW_DATABASE_URL&quot;)
  }
  
  model User {
    id        Int        @id @default(autoincrement())
    createdAt DateTime   @default(now())
    updatedAt DateTime   @updatedAt
    email     String     @unique
    firstName String?
    lastName  String?
    password  String
    playlists Playlist[]
  }
  
  model Song {
    id        Int        @id @default(autoincrement())
    createdAt DateTime   @default(now())
    updatedAt DateTime   @updatedAt
    name      String
    artist    Artist     @relation(fields: [artistId], references: [id])
    artistId  Int
    playlists Playlist[]
    duration  Int
    url       String
  }
  
  model Artist {
    id        Int      @id @default(autoincrement())
    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
    songs     Song[]
    name      String   @unique
  }
  
  model Playlist {
    id        Int      @id @default(autoincrement())
    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
    name      String
    songs     Song[]
    user      User     @relation(fields: [userId], references: [id])
    userId    Int
  }
  </code></pre>