Welcome, to our MERN Stack Tutorial, where we guide you through the process of saving user data in an online database using Express and Mongoose! 🌐💾 Learn how to retrieve and destructure user data from req.body, check for existing email entries, and seamlessly store user information in the database. Join us on this hands-on journey of storing and managing user data with practical examples and Postman testing! 🚀
📚 Resources mentioned in our MERN Course & Don't forget to watch 👇 👉 MongoDB Course: https://youtu.be/rU9ZODw5yvU
Get the Complete Courses in One Video experience right here! 🔗🌟 Get your Free Source Code and Notes now 👉 https://thapatechnical.shop/courses
Join us on this exciting coding journey to master the MERN Stack together! Be sure to hit that subscribe button and ring the notification bell, so you never miss our engaging Hindi tutorials. Your web development skills are about to level up – let's do this!
👍 Feel free to like, share, and ask questions in the comments. We're here to help!
🗨️ Discord Server Link for Programmer to Hangout: https://discord.gg/MdScmCsua6 -----------------------------------------------------
⏳TIMELINE⏳ 0:00 - Introduction 0:50 - Retrieving User Data Using req.body 1:38 - Destructuring the req.body Data 2:25 - Checking if Email Already Exists 5:55 - Storing User Data in the Database if Email Doesn't Exist 6:25 - Testing User Data Storage with Postman
************* 😍 Must Watch Videos For Web Development 😍 *************
Hello, what's up guys? My name is Vinod Balu Thapa
and today in this video we will see
how to store your registration data
in your database. Finally, today's
video is a video that you have been
waiting for a long time that whenever
someone fills their data in the registration form
how will that data be added to the database
so today I am going to tell you that.
Till now, we have not covered the front end
but with the help of Postman, I will show you
how you can achieve that functionality.
So guys, without any delay
let's start the video.
So finally, today in this video
we will see how we will create
a registration form with the help of
Postman and how we will get
that data in our back end
and how in real world
our actual
server
basically how we will store it in the database.
Watch carefully.
Now I have to go to my
auth controller because here my main
logic will come, my main action will be here.
So whenever someone
registers, I mean
I am getting my data in the form of
request.body and that is nothing but
if you go to Postman
this is how I get my data.
We have a username,
we have an email.
Don't give a tense message.
Because I have written a message here.
If I didn't write that message
then I can't directly write request.body
For that, I have to do const
data
equal to request.body
and then we have a body
and then this data
I have to pass like this.
Now if I refresh here
so now I have data
basically I have data, you don't have to worry
I have data like this.
So this username and this email
I can destructure it here.
So instead of getting
this data part, what I will do
I know that I am going to take username
I am going to pass email
because we have
created a user schema
then I am going to have
a phone number
and then at the end we have a password.
I am deleting this.
This way I have data here.
I don't want to show all this data here.
And I have
the steps over here with me
which I want to paste
in front of you.
So get the registration data
with the help of
request.body
This is done.
Now check the email existence
whether the email exists or not.
Let's do it.
Believe me, this is also going to be very useful.
And finally, as I said
world best
our MongoDB course
I showed you how
you can do a lot of things
so that will be useful here.
You have to check whether the email
already exists or not in your database.
I mean it is already registered
in simple language.
So I am going to create a variable
user exists
exists
So I have to write exists
Confusion happens
Guys, we can't do anything.
So first of all, when you
created a model, it means
you have created a new collection.
And creating a collection
means that all your data
is present in that collection.
Which is a very good thing.
So this collection has
access to all the data.
So let's go over here.
So the user I exported here
in our auth-controller
over here, I am getting it.
And let me do one thing.
I am removing this part.
I am also removing this controller.
Because you already know
what it is.
const
I am writing const
user
And I am requiring it.
I think it is dot forward slash
So I have to write
dot forward slash
Models and then we have a user model.
So this user is a collection
which has all the data.
So let me go over here.
See what I am saying here.
If user exists
What is happening here?
It should have been something like this.
So if a user exists
Then
I will use a find method
in my user
dot
And that is find1
And I will
This is an email field.
Let me go over here.
So guys,
You can see clearly here.
We have a field
called email
And here is a random
Once again, this is not my phone number.
This is not my password.
But an email is a field
which has a value.
So if I write an email
like this
So what I am doing is
I am going to pass this email.
This email is my field.
But the second value of this
will be this email.
I am copying and pasting it here.
So basically,
I have passed an email in this collection.
Whatever the user has written.
Whether it exists or not.
Basically,
whether it is already present or not.
If it is present
If it is present,
it means that the user is already registered.
So why do you want to register it again?
So if the user exists
Then guys, you simply
pass a written statement here.
And that is response.status
Here you write
400
And you can write a JSON message here.
And that is
simple message
And that is
Email already exists.
Perfect.
If the email already exists
Then this.
If the email does not exist, then I have to create it.
I have to register the user.
So how will I do it?
await
dot
There are many ways.
But a simple way is to use the create method.
And then
Copy all the data
And then paste it here.
Yeah guys, this is it.
That's all we had to do.
I am doing it in controller save.
I am going here.
Everything is perfect.
Now, where is my registration form?
Because I have not created the front end.
Right?
So I will go over here.
Again, this is my route.
I am using the post method.
I have this content-type.
In which I am telling
that my application is going to be JSON.
And I will add my data in the body.
And then I am going to write JSON in raw.
So it's a username.
It's an email.
Then again we have a phone over here.
So let's go with our phone.
And I am writing a random phone number.
And last we gonna have
our password.
So it's a password.
So I am writing the password.
Alright.
Now we have to see
whether it is properly saved in the database or not.
So I am refreshing.
Email already exists.
How is this happening?
So I am doing it on 2.
Email already exists.
So guys, email is telling me that it already exists.
So let's do one thing.
I am going here.
I am refreshing it.
I have made a connection with this database.
Admin underscore
Look at this.
Finally.
You can see over here.
One second.
Let me show you properly.
So this is the database
which we created.
So guys, our user is ready.
And if I click on users.
So currently
there is no data in this.
There is no data in this.
But look at this.
It is lying to me.
It is telling me that email already exists.
400 bad request.
So I think we made a mistake.
We didn't make any mistake.
It is very simple.
So user.find1
If it gets this email.
Alright.
And by the way, you can write it directly as it is.
If the key and value are same,
then you can write the same value.
So guys, the code is perfect.
And I just realized that
we made a mistake.
Whenever you use find1 method,
you need to wait.
You need to wait a little.
After that, the data will go to it.
And then it will be checked.
If we handle the promise without waiting,
then it becomes difficult for them too.
Now if I refresh.
Boom guys, see internal server error.
So it is an internal server error.
And that is nothing but
directly this part.
And here
the await
should not be like this.
Let me do one thing.
Let me save my part.
Let me refresh it again.
Email already exists.
One second.
Let me refresh it again.
Oh my god.
See this is it.
Database is stored.
It was not a big deal.
I am getting data on console too.
It was just an await.
And yeah,
this is it.
You can do one more thing.
To show JSON data better.
You can show here
like user created.
Wait.
And this user created,
you can directly pass it here.
You can say a message in JSON.
And then
you can pass user created like this.
So again I will go here.
Now this time email will already exist.
Because it already exists.
But if I click on one.
Because I have not created it yet.
And boom guys.
Message is written.
And I got all my data.
Isn't it lovely and awesome?
How easily
we made our registration form.
And I am getting one more data here.
So yeah guys, this is it.
That's all for this video.
And believe me,
the data I am getting
from postman,
I have to do the same
through frontend.
And I don't have to do anything in backend.
It will be in frontend only.
Instead of postman,
it will be in frontend only.
So yeah guys, this is it.
Hope you liked this video.
Like this video.
And next target is 1 million subscribers.
So if you haven't subscribed yet,
please do it.
Till then guys, thank you so much.
Take care.