nestjs

Howto get req.user in services in Nest JS

十年热恋 提交于 2020-05-26 12:16:13
问题 In a controller, I add the user object with a guard, inject some service and call that service to get some response. I have removed a lot of code for brevity. @Controller() @UseGuards(AuthGuard()) export class UserController() { constructor(private readonly userService: UsersService) { } @Get(':id') async findOne(@Param('id') id) { return await this.userService.findOne(id); } } Since I have the AuthGuard , I now know the user is logged in before entering :id route. In the service I would do

Class-validator - validate array of objects

非 Y 不嫁゛ 提交于 2020-05-25 17:22:18
问题 I am using class-validator package with NestJS and I am looking to validate an array of objects that need to have exactly 2 objects with the same layout: So far I have: import { IsString, IsNumber } from 'class-validator'; export class AuthParam { @IsNumber() id: number; @IsString() type: string; @IsString() value: string; } and import { IsArray, ValidateNested } from 'class-validator'; import { AuthParam } from './authParam.model'; export class SignIn { @IsArray() @ValidateNested({ each:

Why @Body() in Post request is not working properly? [Nest.js]

99封情书 提交于 2020-05-24 04:34:24
问题 I'm starting to learn Nest.js, so I am following an Academind Tutorial (link). My code is not working as expected when I try to get the body variable with the @Body() decorator in the POST request. Following this part of the code in products.controller.ts @Post() async addProduct(@Body() body: Product) { console.log(body); const generatedId = this.productService.insertProduct(body.title, body.description, 5.99); return generatedId; } In the terminal the output is just an empty object: {} I

Where and how should I check an access token for validity in passportjs

大兔子大兔子 提交于 2020-05-24 03:55:48
问题 I'm in the process of implementing refresh tokens and I use passportjs. What I don't completely understand is where and how I should check access tokens for validity and in case if an invalid token arrives throw TokenExpiredException . @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor( private readonly authService: AuthService, ) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: process.env.JWT

How to add multiple same type of documents in mongodb

半腔热情 提交于 2020-05-17 07:46:06
问题 I am working on an app where I need to fetch data from collection_1 and submit that same data in collection_2. But I got an error while I am submitting a same document which is already saved in collection_2. Suppose I have saved doc_1 in collection_2 which is fetched from collection_1 and similarly again I saved doc_2 in collection_2, Now again I need to save doc_1 in collection_2 from collection_1 but I didn't get the exact output, am unable to save doc_1 again while doc_1 is already present

How to transfer multiple data from one collection to another using $out aggregation

不问归期 提交于 2020-05-17 06:43:10
问题 I am working on nestjs and I have two collections one is order and the second is payment. Now I need to retrieve one document from the collection order and save it into payment collection which is working properly but the issue is when I am trying to save the second document into payment collection then my first document is overwritten. In other words, the first document has vanished after submitting the second document. I want to save every document in payment collection which i retrieved

NestJS Angular Universal not loading “index.html” automatically on root path

倖福魔咒の 提交于 2020-05-17 06:09:47
问题 I am running an NestJS Angular Universal app on my local server using Angular 9 and Ivy. I can get everything to work when I run: npm run serve:ssr However, nothing loads unless I type the route manually. I would think it would automatically load "index.html" without having to type it in. localhost:8080 ----- nothing localhost:8080/index.html ---- works Is there a way to modify the code to do a rewrite for the root path? I would think this would not be necessary: main.ts import { NestFactory

NestJS Angular Universal not loading “index.html” automatically on root path

谁都会走 提交于 2020-05-17 06:09:31
问题 I am running an NestJS Angular Universal app on my local server using Angular 9 and Ivy. I can get everything to work when I run: npm run serve:ssr However, nothing loads unless I type the route manually. I would think it would automatically load "index.html" without having to type it in. localhost:8080 ----- nothing localhost:8080/index.html ---- works Is there a way to modify the code to do a rewrite for the root path? I would think this would not be necessary: main.ts import { NestFactory

NestJS Views Not gettings added to Dist

血红的双手。 提交于 2020-05-16 20:54:13
问题 My folder structure is similar to below. public views src main.ts /users users.controller.ts /views my-view.hbs /books books.controller.ts /views my-view.hbs This is what i use to add the templates and views const app = await NestFactory.create<NestExpressApplication>( AppModule, ); console.log(join(__dirname, 'public')); app.useStaticAssets(join(__dirname, '..', 'public')); app.setBaseViewsDir(join(__dirname, '..', 'views')); app.setViewEngine('hbs'); hbs.registerPartials(join(__dirname, '..

How to manually test input validation with NestJS and class-validator

大兔子大兔子 提交于 2020-05-16 08:54:28
问题 TLNR: I was trying to test DTO validation in the controller spec instead of in e2e specs, which are precisely crafted for that. McDoniel's answer pointed me to the right direction. I develop a NestJS entrypoint, looking like that: @Post() async doStuff(@Body() dto: MyDto): Promise<string> { // some code... } I use class-validator so that when my API receives a request, the payload is parsed and turned into a MyDto object, and validations present as annotations in MyDto class are performed.