Search code examples
javascriptnode.jsmongodbpostmanuser-registration

Difference between User.register and User.create in mongodb, nodejs


I have been observing both of these codes below, I didn't get what's the difference. Can anyone explain please.

User.register(new User({ username: req.body.username})......

and

User.create(new User({ username: req.body.username}).....

User is the model I have created in mongodb (it stores as in users collection), and I'm going to add new field username. In both the cases, been able to add successfully.


Solution

  • I assume you are using passport-local-mongoose where register() is a convenient helper for creating and setting password for a new user.

    User.register() is from passport-local-mongoose which will insert a new user, if not already exists, using user.save()

    User.create() is from mongoose which internally calls user.save() to insert a user document as well.