我是Nest JS的新手,正在尝试设置我的第一个项目来使用它。但是我有两个实体:组织和用户。我让它正确地为组织工作,但是当我尝试创建用户时,我得到的organizationId为空,无法保存。
这是我的实体
第一个
接下来是我的用户模块
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { User } from './entities/user.entity';
import { OrganizationService } from '../organization/organization.service';
@Module({
imports: [TypeOrmModule.forFeature([User])],
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
这是我的用户控制器
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Roles } from 'src/decorators/roles.decorator';
import { AuthGuard } from '@nestjs/passport';
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
@Post()
@UseGuards(AuthGuard('jwt'))
@Roles('admin')
create(@Body() createUserDto: CreateUserDto) {
return this.userService.create(createUserDto);
}
@Get()
@UseGuards(AuthGuard('jwt'))
@Roles('admin', 'broker', 'assistant')
findAll() {
return this.userService.findAll();
}
@Get(':id')
@UseGuards(AuthGuard('jwt'))
@Roles('admin', 'broker', 'assistant')
findOne(@Param('id') id: string) {
return this.userService.findOne(+id);
}
@Patch(':id')
@UseGuards(AuthGuard('jwt'))
@Roles('admin', 'broker', 'assistant')
update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
return this.userService.update(+id, updateUserDto);
}
@Delete(':id')
@UseGuards(AuthGuard('jwt'))
@Roles('admin')
remove(@Param('id') id: string) {
return this.userService.remove(+id);
}
}
这是我的用户服务
import { Repository } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { IUser } from './interfaces/user.interface';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>,
) {}
async create(createUserDto: CreateUserDto): Promise<IUser> {
console.log('Created User DTO: ', createUserDto);
const createdUser = this.userRepository.create(createUserDto);
console.log('Created User: ', createdUser);
return await this.userRepository.save(createdUser);
}
findAll() {
return `This action returns all user`;
}
findOne(id: number) {
return `This action returns a #${id} user`;
}
update(id: number, updateUserDto: UpdateUserDto) {
return `This action updates a #${id} user`;
}
remove(id: number) {
return `This action removes a #${id} user`;
}
}
当我点击创建端点时,控制台记录了以下内容:
Created User DTO: {
id: '6104ac51c617d0d6eb',
organizationId: '4LYTspbph',
email: 'test@test.com',
role: 'admin',
name: 'test name'
}
Per - First response tried that solution and get this with same error message.
Created User: User {
id: '6104ac51c617d0d6eb',
organizationId: '4LYTspbph',
email: 'test@test.com',
role: 'admin',
name: 'test name'
}
然后在postgres数据库中插入时返回以下内容:query failed: INSERT INTO "user"("id", "email", "role", "name", "createdAt", "updatedAt", "organizationId") VALUES ($1, $2, $3, $4, DEFAULT, DEFAULT, DEFAULT) RETURNING "role", "createdAt", "updatedAt" -- PARAMETERS: ["6104ac51c617d0d6eb","test@test.com","admin","test name"] error: error: null value in column "organizationId" of relation "user" violates not-null constraint
我不知道为什么要从传递给函数的数据中删除organizationId。有人能帮助我吗?这一定是件简单的事情。
我试过导入组织模块,并在构造函数中导入一个新的仓库,但同样出错。我试过在用户模块的forFeature导入中添加Organization。
1条答案
按热度按时间nfs0ujit1#
这里的组织是一个实体,typeorm不允许你只插入ID,而是需要插入整个数据。