Reputation: 5226
I'm a front-end developer who starts nest for my small project backend. I'm trying to write a user signup module and I want to check my MongoDB to see if we already have a user with current PhoneNumber in our DB or not.
async addUser(createUserDTO: CreateUserDTO): Promise<IUser> {
const UserWithSamePhoneNumberList: IUser[] = await
this.findUserByPhoneNumber(createUserDTO.phoneNumber);
if(UserWithSamePhoneNumberList.length > 0) {
console.log("repeative phone number");
}
const hash:string = await bcrypt.hash(createUserDTO.password, this.saltRounds);
const userDTo: CreateUserDTO = {
name: createUserDTO.name,
password: hash,
phoneNumber: createUserDTO.phoneNumber,
signupDate: new Date().toString()
};
const newUser: any = new this.userModel(userDTo);
return newUser.save();
}
async findUserByPhoneNumber(phoneNumber:string): Promise<IUser[]> {
return this.userModel.find({phoneNumber:phoneNumber});
}
i write the code and it log the repeative phone number
successfully but my problem is i dont know what is the standard way to throw exception of repeative phonenumber and send standard response to client
Upvotes: 0
Views: 114
Reputation: 318
you can simply throw the exception, nest will attach proper status code automatically
if(UserWithSamePhoneNumberList.length > 0) {
throw new ConflictException({errorObject})
}
Upvotes: 1