When creating a new user, im checking for a duplicate first via:
const duplicate = await User.findOne({ username }).lean().exec();
I am confused as to why the coding tutorial im doing attaches the exec method. From what i read, this checks for a match in the string and will return any matches in an array. So on a post req, id get a username, findOne (i believe) will already match the req.body.username to a username inside my User schema/DB, hence i dont understand why the person running the tutorial also adds the .exec method. im sure something is flying over my head.
Tried reading mdn docs on exec method
CodePudding user response:
In that line of code:
Model.findOne creates and returns a query object, which can be further acted on.
The lean method modifies that query, and returns the modified object.
The exec method executes the query asynchronously and returns a promise.
await
waits for the promise to resolve, and returns the final value, which is then assigned to the duplicate
variable.