1

i'm trying to make a signup methode in nestJs. But the matter is that when i use my POST request to create a new user, my database don't add this new user in her list of users.

I created this method:

createUser(data: AuthDto){
        const user = {
            pseudo: data.username,
            password: data.password,
            pointEffort: 10,
            roleId: 2,
            id: 5
        };
        console.log(user);
        const result = this.usersRepository.create(user);
        return result;
    }

The communication with the database works cause i can get the user i already created in phpmyadmin when i insert them with an SQL request.

Here are the entity in my nestJs:

import { Place } from "src/place/place.entity";
import { Science } from "src/science/science.entity";
import { TechnologieUser } from "src/technologie/technologieUser.entity";
import { Column, Entity, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Role } from "./role.entity";

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: Number;

    @Column()
    pseudo: String;

    @Column()
    password: String;

    @Column()
    pointEffort: Number;

    @ManyToOne(type => Role, role => role.users)
    role: Role;

    @ManyToMany(type => Science, science => science.users)
    scienceTab: Science[];

    @OneToMany(type => TechnologieUser, technologieUser => technologieUser.user)
    technologieTab: TechnologieUser[];

    @OneToMany(type => Place, place => place.user)
    inventaire: Place[];
}

And here the user table: enter image description here

So when i click on a button to create a user, my request get sent and return the new user in the console.log but i never see this new user in my database. Thx for your help

djodjoxyz
  • 11
  • 2

1 Answers1

1

From docs:

create - Creates a new instance of User. Optionally accepts an object literal with user properties which will be written into newly created user object

const user = repository.create() // same as const user = new User();

If you want to save the object try this:

const result = this.userRepository.save(this.usersRepository.create(user));
  • 1
    Thx a lot, i'm feeling so dumb right now. All tutorials i have seen talk about create and not save, so i've stuck my mind on this – djodjoxyz Jan 04 '23 at 15:58