0
import otpGenerator from 'otp-generator';
import date from 'date-and-time';
import { PrismaClient } from '@prisma/client';


const prisma = new PrismaClient();

const generateOtp = async () => {
    let otpCode = otpGenerator.generate(4, {lowerCaseAlphabets: false, upperCaseAlphabets:          false, specialChars: false});

    let existingOtp = await prisma.otp.findUnique({
      where: {
          code: otpCode
      }
    
    },);

      while(existingOtp) {
        otpCode = otpGenerator.generate(4, { lowerCaseAlphabets: false, upperCaseAlphabets:     false, specialChars: false});
        existingOtp = await prisma.otp.findFirst({
          where: { code: otpCode }
        });
      }

      const now = new Date();

      const otp = await prisma.otp.create({
        data: {
          code: otpCode,
          createdAt: now,
          expiryDate: date.addMinutes(now, 30)
        }
      
      });


     return otpCode;

};

All the lines containing code otpCode gives this error:

Type '{ code: string; }' is not assignable to type 'OtpWhereUniqueInput'. 
Object literal may only specify known properties, and 'code' does not exist in type 'OtpWhereUniqueInput'.ts(2322) index.d.ts(2403, 5):
The expected type comes from property 'where' which is declared here on type '{ rejectOnNotFound?: RejectOnNotFound | undefined; select?: OtpSelect | null | undefined; where: OtpWhereUniqueInput; }'

This is the Prisma schema:

generator client {
    provider = "prisma-client-js"
}

generator nexusPrisma {
   provider = "nexus-prisma"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id        String  @id @default(cuid()) @map("_id") @db.ObjectId
  email     String  @unique
  name      String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Otp {`your text`
  id        String  @id @default(cuid()) @map("_id") @db.ObjectId
  code      String  @id @unique
  createdAt DateTime @default(now())
  expiryDate DateTime
}

I tried a lot of things and it did not work but when I changed the file to a Javascript file there were no errors.

Daedalus
  • 7,586
  • 5
  • 36
  • 61

0 Answers0