#**Decorator** is not a function

12 messages · Page 1 of 1 (latest)

autumn grail
#

I'm trying to implements public routes for my app.
I create a Public decorator : export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
I've two controllers, I can add the decorator on method's of one of my controller but if I put it on the other one, the app throw TypeError: (0 , primary_1.Public) is not a function
What can happen ?

night yew
#

You import it with import { Public } from '../path/to/file'; right?

autumn grail
#

Sure, i'm using barrels

import { Public } from 'src/authentication/primary';
export * from './public.decorator';
night yew
#

What controller is importing the@Public() decorator that is having an issue? Could it be a circular file reference?

autumn grail
#

@Public() import only SetMetaData and is used only in the "gulty" controller. I can also use the @SetMetaData() decorator directly on my method but my @Publicdecorator that only call SetMetaDatathrow...

night yew
#

What is the "guilty" controller?

autumn grail
#
import { BadRequestException, Body, Controller, Inject, Post } from '@nestjs/common';
import { ApiBearerAuth, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { Public } from 'src/authentication/primary';
import { UserRepository, User, USER_REPOSITORY } from '../domain';
import { CreateUserRequest, UserResponse } from './rest';

@ApiBearerAuth()
@ApiTags('User')
@Controller('user')
export class UserController {
  constructor(@Inject(USER_REPOSITORY) private userRepository: UserRepository) {}

  @Public()
  @Post()
  @ApiOkResponse({ type: UserResponse })
  async create(@Body() userRequest: CreateUserRequest): Promise<UserResponse> {
    const existingUser = await this.userRepository.findByMail(userRequest.mail);
    if (existingUser) throw new BadRequestException();

    const user = User.builder()
      .withNickname(userRequest.nickname)
      .withMail(userRequest.mail)
      .build();
    const savedUser = await this.userRepository.save(user);

    return new UserResponse(savedUser);
  }
}
night yew
#

Does src/authentication/primary for whatever reason, ever import this file?

#

My thought is that Public is being imported as undefined because of a a circular file reference

#

We could verify that by importing @Public() from its file directly

autumn grail
#

Oh ok, I think i understood
authentication.module import user.module
beginner mistake...

night yew
#

Ah, yep, that could create a circular file reference which could cause the decorator to be undefined on import.