Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用NestJS的未处理承诺拒绝_Javascript_Node.js_Nestjs - Fatal编程技术网

Javascript 使用NestJS的未处理承诺拒绝

Javascript 使用NestJS的未处理承诺拒绝,javascript,node.js,nestjs,Javascript,Node.js,Nestjs,当我执行下面的代码时,我从服务器得到“201 Created”响应,但实际上数据并没有插入到服务器中 我在我的应用程序中使用带有TypeORM和Postgres的nestJS import { Repository, EntityRepository } from "typeorm"; import { User } from './user.entity'; import { AuthCredentialsDto } from './dto/auth-credentials

当我执行下面的代码时,我从服务器得到“201 Created”响应,但实际上数据并没有插入到服务器中

我在我的应用程序中使用带有TypeORM和Postgres的nestJS

import { Repository, EntityRepository } from "typeorm";
import { User } from './user.entity';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
import { ConflictException, InternalServerErrorException } from "@nestjs/common";

@EntityRepository(User)
export class UserRepository extends Repository<User>{
    async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void>{
        const {username, password} = authCredentialsDto;

        const user = new User();
        user.username = username;
        user.password = password;

        try{
            await user.save();
        } catch (error) {
            if (error.code === '23505'){ // error code for duplicate value in a col of the server
                throw new ConflictException('Username already exists');
            } else {
                throw new InternalServerErrorException();
            }
        }      
    }
}
控制器模块代码如下:

import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Controller('auth')
export class AuthController {
    constructor( private authService: AuthService){}

    @Post('/signup')
    signUp(@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto): Promise<void>{
        return this.authService.signUp(authCredentialsDto);      
    }
}
从'@nestjs/common'导入{Controller,Post,Body,ValidationPipe};
从“/auth.service”导入{AuthService};
从“./dto/auth-credentials.dto”导入{AuthCredentialsDto};
@控制器('auth')
导出类AuthController{
构造函数(私有authService:authService){}
@Post(“/signup”)
注册(@Body(ValidationPipe)authCredentialsDto:authCredentialsDto):承诺{
返回此.authService.signUp(authCredentialsDto);
}
}
你好

我认为您在这里遇到的问题是没有等待对AuthController中的authService.signup()方法的方法调用

因此,修正应为:

在AuthService::signup()方法中: 在AuthController::signup()方法中:
您的代码中是否有不等待响应的地方?或者您是否使用
res.send()
而不是从
控制器返回值?您可以添加控制器的代码吗?@JayMcDoniel感谢您的回复。我已经编辑了帖子,并在帖子中添加了控制器代码,如果这有助于您解决我的问题的话。@yash感谢您的回复。我已经编辑了帖子,并在帖子中添加了控制器代码,如果这有助于解决我的问题。。
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Controller('auth')
export class AuthController {
    constructor( private authService: AuthService){}

    @Post('/signup')
    signUp(@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto): Promise<void>{
        return this.authService.signUp(authCredentialsDto);      
    }
}
return await this.userRepository.signup(authCredentialsDto);
return await this.authService.signup(authCredentialsDto);