nestjs
How to fix AXIOS_INSTANCE_TOKEN at index [0] is available in the Module context
Import HttpModule from @nestjs/common in TimeModule and add it to the imports array. Remove HttpService from the providers array in TimeModule. You can directly import it in the TimeService. import { HttpModule } from ‘@nestjs/common’; … @Module({ imports: [TerminalModule, HttpModule], providers: [TimeService], … }) TimeService: import { HttpService } from ‘@nestjs/common’; If your response type … Read more
how to send error codes in nestjs app from controller?
You should use exception filters in your case instead. In your case you’ll need: throw new BadRequestException(error); Or you can use throw new HttpException(‘Forbidden’, HttpStatus.FORBIDDEN); That will return { “statusCode”: 403, “message”: “Forbidden” } Docs: https://docs.nestjs.com/exception-filters
TypeORM CLI: No changes in database schema were found
For me this hint helped. Just run npm run build before generating with npm run typeorm — migration:generate -n migration_name the new migration. Somehow an up to date dist folder needs to be present.
validate nested objects using class-validator in nest.js controller
Try specifying the nested type with @Type: import { Type } from ‘class-transformer’; export class CurrencyDTO { @ValidateNested({ each: true }) @Type(() => Data) data: Data[]; } For a nested type to be validated, it needs to be an instance of a class not just a plain data object. With the @Type decorator you tell … Read more
What is the right way of production deployment of nestjs application
Own server 1) Checkout your project’s repository on your server and run npm install. 2) Run npm run build which compiles your project to javascript: rimraf dist && tsc -p tsconfig.build.json 3) Start your application with: node dist/main.js Serverless zeit now See this answer. Heroku 1) Add the file Procfile to your project’s root directory: … Read more
Optional authentication in Nest.js with @nestjs/passport
You can just create your own AuthGuard for example by extending the existing one: export class OptionalJwtAuthGuard extends AuthGuard(‘jwt’) { // Override handleRequest so it never throws an error handleRequest(err, user, info, context) { return user; } } And then use this one on your controllers instead: @UseGuards(OptionalJwtAuthGuard)
NestJS returning the result of an HTTP request
This issue comes from the axios library. In order to fix that, you have to pull out the data property: return this.httpService.post(…) .pipe( map(response => response.data), );
Test functions cannot both take a ‘done’ callback
You are combining Async/Await and Done. Either use asnyc/await, or done. it(‘throws an error if user signs up with email that is in use’, async () => { try { await service(); expect(…); } catch (err) { } }); or use the done format it(‘throws an error if user signs up with email that is … Read more
Is it possible to set default values for a DTO?
You can set default directly in your DTO class: export class MyQuery { readonly myQueryItem = ‘mydefault’; } You have to instantiate the class so that the default value is used. For that you can for example use the ValidationPipe with the option transform: true. If the value is set by your query params it … Read more