As per code below inside the "localRegister" function, I added the 'userId' and 'accountType' to the createUserObj object using the bracket notation, so it can satisfy the properties of the User class type.
But I am still getting that message saying that User is missing the userId, accountType properties.
Seems like a simple issue that I just couldn't figure out...how do we make Typescript understand that those properties has been added already, or is there another proper way to add properties?
Thanks in advance.
user.service.ts (partial code)
async localRegister(createUserDto: LocalUserDto) {
const passwordHash = await bcrypt.hash(createUserDto.password, 10);
let createUserObj = { ...createUserDto, password: passwordHash };
createUserObj['userId'] = uuid();
createUserObj['accountType'] = 'local';
this.userCreate(createUserObj); // <====== typescript error here
// Argument of type '{ password: string; username: string; email: string; }' is not assignable to parameter of type 'User'.
// Type '{ password: string; username: string; email: string; }' is missing the following properties from type 'User': userId, accountType
}
async userCreate(userObj: User) {
const createdUser = new this.userModel(userObj);
try {
await createdUser.save();
} catch (err) {
throw new NotFoundException(err.message);
}
}
user.schema.ts (where User type is defined)
@Schema()
export class User {
@Prop({ required: true })
userId: string;
@Prop({ required: true })
username: string;
@Prop({ unique: true })
email: string;
password: string;
@Prop({ required: true })
accountType: string;
}