How can I define the arg type in a function as a string from list of strings and run a mongodb query. What I am doing is given below:
users.services.ts
async findOne(key: "_id" | "email" | "username", value: string) {
const user = await this.userModel.findOne({ key: value }).exec();
if (!user) {
throw new NotFoundException();
}
return user;
}
findOne in users.services.tsconst user = await this.usersService.findOne("username", username);
Its not working and when debugging its not picking the values of key & value.
Many thanks for your help in advance.
You need to use $or query
{
$or: [
{"_id": value},
{"email": value},
{"username": value}
]
}
It's better to have three/more different functions and make a call with the given key than having list of or conditions.