ethan0905 / ft_transcendence

Wiki with step-by-step explained strategy on how to: create app (front+back+infra), 42auth, 2FA and more..
2 stars 0 forks source link

'accessToken' property does not exist in type... #21

Closed ethan0905 closed 1 year ago

ethan0905 commented 1 year ago

I verified multiple time if the schema was well exported (using npx prisma migrate dev and npx prisma generate) but the error stays. when i exported the migrations with the accessToken and refreshToken variables:

The following migration(s) have been created and applied from new schema changes:

migrations/
  └─ 20230316143206_added_access_token_and_refresh_token/
    └─ migration.sql

Your database is now in sync with your schema.

✔ Generated Prisma Client (4.10.1 | library) to ./node_modules/@prisma/client in 214ms

server side error:

backend_nestjs    | src/auth/auth.service.ts:218:6 - error TS2322: Type '{ id42: any; email: any; username: any; accessToken: string; refreshToken: string; }' is not assignable to type '(Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput)'.
backend_nestjs    |   Object literal may only specify known properties, and 'accessToken' does not exist in type '(Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput)'.
backend_nestjs    |
backend_nestjs    | 218      accessToken: access_token,
backend_nestjs    |          ~~~~~~~~~~~~~~~~~~~~~~~~~
backend_nestjs    |
backend_nestjs    |   node_modules/.prisma/client/index.d.ts:1954:5
backend_nestjs    |     1954     data: XOR<UserCreateInput, UserUncheckedCreateInput>
backend_nestjs    |              ~~~~
backend_nestjs    |     The expected type comes from property 'data' which is declared here on type '{ select?: UserSelect; include?: UserInclude; data: (Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput); }'

schema.prisma:

model User {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt

  email     String  @unique
  username  String? @unique
  avatarUrl String?

  accessToken String?
  refreshToken String?

  [...]
}

auth.service.ts:

    async create42User(access_token: string, resfresh_token: string, user42: any) {
        try {
            const user = await this.prisma.user.create({
                data: {
                    id42: user42.id, //gonna remove this later
                    email: user42.email,
                    username: user42.login,
                    accessToken: access_token, // error here
                    refreshToken: resfresh_token, // error here
                },
            });

            console.log("User has been created! \n");

            return user;
        }catch (error) {
            if (error instanceof PrismaClientKnownRequestError) {
                if (error.code === 'P2002') {
                    throw new ForbiddenException('Credentials taken');
                }
            }
        };
    }
ethan0905 commented 1 year ago

I tried: 1- changing the var names (nothing changed) 2- to give my token.access_token to my username (the name is created with the token string) 3- to add the @unique decorator (still not removing the error)

ethan0905 commented 1 year ago

The problem seems to have been related to node_modules, not allowing to use those newly created datas for some reason... A stop of the docker + make clean_modules + make command was enough to fix the issue.