nestjs

How to return a 404 HTTP status code when promise resolve to undefined in Nest?

折月煮酒 提交于 2021-02-08 15:13:45
问题 In order to avoid boilerplate code (checking for undefined in every controller, over and over again), how can I automatically return a 404 error when the promise in getOne returns undefined? @Controller('/customers') export class CustomerController { constructor ( @InjectRepository(Customer) private readonly repository: Repository<Customer> ) { } @Get('/:id') async getOne(@Param('id') id): Promise<Customer|undefined> { return this.repository.findOne(id) .then(result => { if (typeof result ===

How to return a 404 HTTP status code when promise resolve to undefined in Nest?

纵饮孤独 提交于 2021-02-08 15:12:21
问题 In order to avoid boilerplate code (checking for undefined in every controller, over and over again), how can I automatically return a 404 error when the promise in getOne returns undefined? @Controller('/customers') export class CustomerController { constructor ( @InjectRepository(Customer) private readonly repository: Repository<Customer> ) { } @Get('/:id') async getOne(@Param('id') id): Promise<Customer|undefined> { return this.repository.findOne(id) .then(result => { if (typeof result ===

Use the same class as Input and Object type in GraphQL in NestJS

拜拜、爱过 提交于 2021-02-08 13:42:11
问题 I am trying to setup my graphql resover to handle an array of objects but cant get the @Args decorator configured. I created my own ArgsType import { ArgsType, Field, Int, ObjectType } from '@nestjs/graphql'; @ArgsType() // to be used as type in the resolver @ObjectType() // for schema generation export class Club { @Field(type => String) president: string; @Field(type => Int) members?: number; } Resolver with adding a single Club works just fine! @Query(() => Int) async addClub(@Args() club:

sequelize-typescript querying if string contains word

六眼飞鱼酱① 提交于 2021-02-08 11:53:22
问题 I m using Nestjs + sequelize-typescript + postgresql. I would like to select rows in table that content contains specific word. this is my code : async findUsers(searchedWord: string): Promise<users[]> { return await this.USER_REPOSITORY.findAll({ attributes: [ 'id', 'name' ], where: { content: '%'+ searchedWord +'%' }, order: [ [ 'id', 'ASC' ] ] }); } } but this method didn't work ! Any suggestions ? 来源: https://stackoverflow.com/questions/59533244/sequelize-typescript-querying-if-string

How to provide server-side pagination with NestJS?

血红的双手。 提交于 2021-02-08 11:03:18
问题 Given a MEVN stack using Nestjs, MongoDB(mongoose) I am working to setup server-side pagination. My approach is to use the mongoose-aggregate-paginate-v2, but I have not been able to distill what I need from my research 1 to make this work within the framework of Nestjs(typescript) and mongoose. Thanks for the assist.. Following documentation on Nestjs mongoose models, and mongoose-aggregate-paginate-v2 setup, I have the following: contact.provider.ts import mongoose, { Connection,

How to serialize a nest js response with class-transformer while getting data with Typegoose?

帅比萌擦擦* 提交于 2021-02-07 20:13:34
问题 I have been trying to work through the NestJs example for the Serialization Section for Mongodb using Typegoose using the class-transformer library. The example given at https://docs.nestjs.com/techniques/serialization only shows how to use serialization in TypeORM. I followed the same process for Typegoose. Here is what I have tried so far. // cat.domain.ts import { prop } from '@typegoose/typegoose'; export class Cat { @prop() name: string; @prop() age: number; @prop() breed: string; } //

NestJS JwtStrategy use configService to pass secret key

爱⌒轻易说出口 提交于 2021-02-04 16:08:45
问题 I have the JwtStrategy class from docs example (https://docs.nestjs.com/techniques/authentication): @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor( private readonly authService: AuthService, private readonly configService: ConfigService, ) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: this.configService.getSecretKey, }); } // ... } When I am trying access this before calling super() I get an error. But I still want to

Nestjs can't resolve dependencies of XModel

こ雲淡風輕ζ 提交于 2021-01-29 22:42:55
问题 I have been working on this app for like 3 months which is near completion. But since yesterday I have been unable to solve this problem. I want to use activityTypeService in activityLogService and I have been getting this wired error. I have already exported activityTypeservice in its module. see below below is ActivityTypeModule, I export ActivityTypeService so it can available in ActivityLogService @Module({ imports: [MongooseModule.forFeature([{ name: 'ActivityType', schema:

Nestjs can't resolve dependencies of XModel

谁说我不能喝 提交于 2021-01-29 22:38:36
问题 I have been working on this app for like 3 months which is near completion. But since yesterday I have been unable to solve this problem. I want to use activityTypeService in activityLogService and I have been getting this wired error. I have already exported activityTypeservice in its module. see below below is ActivityTypeModule, I export ActivityTypeService so it can available in ActivityLogService @Module({ imports: [MongooseModule.forFeature([{ name: 'ActivityType', schema:

How to inject interface in module of nestjs app

删除回忆录丶 提交于 2021-01-29 20:14:54
问题 Here is the screenshot of error: I got many answers for this problem but i didn't get any exact proper solution.When i remove UsersModule from below code then i got error of 404 not found in postman.But when i write UsersModule in below code then i got error which is mentioned in screenshot. Here is the code of app module: @Module({ imports: [UsersModule,MongooseModule.forRoot("mongodb://localhost:27017/jwt",{ useNewUrlParser: true })], controllers: [AppController], providers: [AppService] })