Hello what's up guys, my name is Pinar Padu
and today in this video we will see
how to store data in database in Hindi
so let's start
first of all guys
I would like to welcome you all
to my channel Tapa Technical Pen
I would like to tell you guys that today's video is going to be very awesome
because finally guys
whatever registration form we have created
although we have not yet created the UI
we are still working on our backend
but very soon this UI will also be created
but the most important thing is
that whenever the user will register
whether that registration
is in the form of React UI
or if you remember
we have done it through the backend
through postman, you can see here
so whenever the user will register
by filling all these details
and then when he will click on send
or when we work on UI
and when we click on register
so guys whatever data the user has filled
whatever data the user has filled
we will store that on our database
and boom guys
you can see the data is stored on our database
so I am going to tell you how to delete it
one second
let me zoom in for you guys
one more time
this is the name of our database
already with the help of user schema
basically simply schema and models
we have created this collection
and we have defined our document
if you have not checked out the video yet
then do check it out
today
how a user fills his data
and clicks on send
if we talk about this route
that is the post method
so how to store the data on the database
we will see that query
and this is very important
and it is going to be fun
if I click on send
using postman
it is same like
I have created a form through react
and clicking on register
and clicking on send
but I know you must be thinking
that this form of react
this ui
how is it working
you will get to know that in the next few videos
but our back end is getting complete
alright guys
let's start the video
but before that
if you have not joined my telegram group
then do join
you can see I have added it on Instagram
and please follow me on Instagram
I don't want much
I want 10k followers
so that I can share my links
in front of you
and I hope you will follow me
please
and one more thing
if you have not subscribed my channel
then please subscribe
you can see the bell icon on the right
click on it
so that you don't miss daily awesome videos
and the minimum requirement of today's video
is the complete javascript tutorial
in Hindi in 2021
which I have worked hard for 17 hours
and created for you
please check out this video
because I have told you a lot about promises
so those promises are going to be used today
so guys let's start the video
without any delay
I want to tell you that today
we will store the data on the database
with the help of promises
and in the next video I will tell you
how you can do it with the help of async
and I think where is the folder
alright
we have seen so much in the previous video
and if you don't know
what we have seen in the previous video
then please check out this playlist
you can't miss a single video
yes guys
you can't miss a single video
you can see everything from the beginning
you will enjoy it
alright guys
this is my register part
and I have shown you
if I take you here
I have already told you about the postman
ok
let's run the server
cd server
cls
noteman app.js
ok
connection is successful
with mongodb atlas
and this is my register
ok
my method is post
it means user is filling the data
I have to get it
and the moment I click on send
I see all the data
which the user has filled
I am getting it
this is a very good thing
I am getting the data on my postman
and I am getting it on my console
but now what I want
whatever data I have filled
I want it on my database
which is very important
and once the data is stored on the database
then we can retrieve that data anytime
we can use it anywhere
it will be fun
it will take email and password
once the data is stored on the database
our work will be very easy
so we are going to see that today
with the help of promises
in the next video we will see through async await
now what we are doing
whatever data the user has filled
in the postman
because we have not created the original file yet
and whatever the user has written here
to get it, the same code will be there
so it's not like we are writing a different code for the postman
and a different code for this
here the data that I am sending on the postman
and guys when the time comes
through react
the UI that you can see
and when we fill the data
and when we click on register
we have to write a code
with which whatever data the user has written
we are going to get it here
and to get that data
the same code will be there
we don't have to change anything here
just write a few codes in react
that's it
and you can understand that
whatever I have filled in the postman
you can understand that I have filled it here
because both are the same
there will be a difference of 4-5 lines
and nothing else
I think I have talked too much
but it was very important to tell you all these things
otherwise you would have been thinking
that I need react through
that will also come
I have already told you on request.body
that I am getting the data
what I have to do with this data
I have to store it on request
how will it be now
it is very simple
I have kept it as it is
and I have told you
we will use the promises
and if you don't know what is promises
then you have to watch this video
in which I have explained it very well
so first of all
instead of request.body
I want only the user's name
and then email
only name and email
do you know what I can do for that
request.body
and
name
I can copy it one more time
because there is no limit of the console
that you can do it as many times as you want
I will save the controller
now watch carefully
it is normal here
I will go here and click on send
the moment I will get all the data
and guys you can see
I have got a lot of data
here it is
because I have got
name and email
I hope you are getting it
and there is another way to write it
there is another way to write it
because we have seen it in ES6
ECMAScript 2015
and again for that you have to watch this video
in which I have explained ECMAScript
asynchronous, you will get everything
you will get it complete in this video
ECMAScript 2015
in ES6 by object destructuring
we have got a property
and that is super duper easy
suppose what will happen tomorrow
for name I have to write
request.body.name
request.body.email
in the same way I have to write phone and work
password and then confirm password
so for all these
request.body.
you can understand how hectic it is
so why not something like
I just write name and I get my name
as I am getting it
so how can I do it, I can't do it
but after ECMAScript 2015
I can do it very easily
const
this is my object destructuring
and here simply I will write
request.body
and now
here it will be like name
email, spelling you have to write it right
that's all you have to remember
and I am saying that because
exactly whatever it is
we have defined here
so it is very important to match
like phone is phone
it is very important to match
so name, email
and then simply
phone number
then work
then password
and then confirm password
control s save
now I know what to do
I just have to copy this
and now simply name
and then simply
email
and click on send
sending request as it is
because I am not doing anything here
and I am not showing anything on response.json
that's why it is sending
but the best part is
I wanted to show you this
that is vinod and then thapa technical
I mean thapa.technical.com
so earlier
request.body.name
request.body.email
now simply name
and then email
whatever the user is filling
whether it is on my postman
or on my react form
I can get it
so it was very simple
now I will keep it as it is
now what I have to do
I have got all the data
now I will remove all this
I will try to keep it as neat and clean as possible
I will also remove this
now see
whatever user has filled the data
I have got it very easily
now what I have to do
use validation
I don't want user to leave any field
empty so I want to show them error
and I know
some of you asked me on telegram link
that how to do email validation
whether it is front end or back end
so guys if you are watching that video
and if you have that doubt then I hope
you will get to know
it is very simple
if
so
I will copy all this
this part
I will copy it
ctrl d ctrl d ctrl d ctrl d
and tick
and here I will add it
so how easily
everything is done
and now whatever you can see
I will copy it again
I will use OR operator
and now what is this
OR operator
OR this OR this
OR this
if any one of these is not there
it will give error
and again if you don't know the difference between OR operator
and AND operator
then I highly recommend you to watch this video
because it is advanced
we can't go into small details
so watch this video
you will get to know everything
so if user
didn't fill any field
then we will return it directly
no need to see further
just fill data
so response.status
is very important
for now I won't give it
so that I can show you
what happens if you don't write
and here
it is very simple
now we have error
one second
I will go to error
you can write anything
I am writing normal
I will save controller
at least I will try once
I will go here
let's say I didn't fill name
I didn't keep name
now if I click on send
and boom guys see error
please fill the fill
I mean both are same
in spelling
but at least I got error
I told you intentionally
that I am not going to write status code
and I will tell you the reason
here you won't get data
because we are not seeing anything
but the thing to see
even if it is an error
and if you remember
errors are mostly
if we see the status code
maybe it can be client error or server error
but the range of error
starts from 400
and again if you don't know
I won't tell you to watch the video
I already have it here
I will share the link
redirect client error
and server error
server has nothing to do
client error
client user
didn't fill the data
so what we have to do
we will go here
but I will add this
request was well formed
but was unable to followed due to semantic errors
means request is all right
there is no problem here
but the problem is
client didn't fill the data
so I will go here
response
status simple
response.status
before we used to write individually
but now we can write together
here I will put dot
and here 422
422 means this error
now I will save the controller
I will go to postman
and finally
finally you can see
earlier it was 200
but now it is 422
unprocessable entity
and error name is same
as before
at least we have done this much
we have seen the validation
now I have to store the data
most important thing
before storing the database
is to check
whether the user already exists
because we are registering the user
first time user is going to register
simple means
user never registered before
means his email
and mobile number must be unique
so I will show you
I will go here
now I will get two things
and that is I will get my database
connection part
so I will go here
single quote double quote
forward slash database forward slash
my connection
here in my user schema
I have exported the user
because I have mentioned everything in it
so what I have to do
basically I have to require
so const my user
will be equal to require
here I will write require
perfect
here I have to write dot dot forward slash
models forward slash user schema
this is it
now this user has all the data
with the help of this I can do a lot
now see carefully
I can find this user
find one
we have already seen this method in mongodb
please check out that video
I will provide the link in the description
I will go here
parenthesis
now I will go with curly braces
already this email
I mean whatever email is on my database
whatever email we have defined
of the user
does that email receive mail from this email
or not
this is the email field of the database
now what I am going to write on the right
that will be the email
which the user is filling while registering
if this email
matches
it means that the email already exists
because it should not match
because it is a matter of registration
so what I will do
I will go here dot and then then
because it was returning the promise
please check out the video
promise has only two things
if you promise
your friend that
exactly one year later
I am going to meet you at this place
what will happen after one year
there will be only two things
probability is 50-50
here you will go to meet or not
one year later you will meet him at that place
or you will not meet
and nothing can happen
if you request
on the database
that yes database
that email is filled by the user
matches or not
obviously there will be two reasons
yes or no
yes means fulfilled
no means rejected
if it is fulfilled
then we will get the response
I can simply write here
user exists
or if you don't like it
you can directly write the response
but I am deliberately writing
user exists
it means
the user is already present
here you know what to do
when you write it
here you have to paste
the fat arrow function
inside the then
and if
the user exists
if that email
matches
then we have to
get out of the application
and tell the user
that this number is already registered
I mean this email id
here you have to write
email already exists
email already exists
simple
at least we have checked
this much
now I will keep it as it is
now you have to see
very carefully
if the user does not exist
it means
the user is registering for the first time
at that time
you have to create
your own document
for that
we have a method
const
new user
new
with the help of new keyword
I am going to create an instance
and if I want
I can write request.body
I have written this much
and if I simply write.save
it will reduce
but sometimes what we want
even if there are 100 fields
but I have to add only 3 data
I can't do it by request.body
because request.body
it means all the data
all the data that the user is filling
will go automatically
which is not right
so what I will do
whatever data you can see
I will copy it
and paste it here
and that's it
actually it doesn't look like this
it looks like this
you understand what I am saying
actually it looks like this
because
this name
on the left side
as a field
and on the right side
name
which data the user has filled
in the input
but both are same
email, email, email
name, name
and if both the key and the value are same
then no need to write both at the same time
you can write any one
it will work
so again you can say
this is also a new property of object
which was introduced in ECMAScript 2015
here we can't say destructuring
but this is a property
if key and value are same
you can write any one
now one thing you have to keep in mind
that this part
is not returning any promise
so I will keep it as it is
I will give a gap of one line
now this user
has filled all the data of user
now it's like your one new document
so what you have to do
you have to save it
in your user collection
so what I do
you have to call user.save()
now a very important thing
again this method
it returns promises
it means
again you have to use then
now with the help of then
you can check
that user's data is successfully registered or not
now I am going to write
the status part first
because you already know
it means data is created on database
and then I want to show a message through JSON
and that is
simple alright message
you can write anything
successfully
I will save it
and then I will
.catch()
it means if there is any error
then this part
will catch it
I could have written it very short
in fat arrow function
but I am writing it very long
so that you don't have any problem
response.status()
I will write here
same 500 because now
it is error of database
and here
I will write.json
here curly braces
and this is it
now you can write your error
failed registered
I think registered will be enough
that's all
and one more thing
you can see
when we were running command
if this command is not running
I mean if there is any error
so I will write here
I can catch it very easily
so catch()
I will write here
now I don't need to write parenthesis
if you have only one argument
I mean only one parameter
and I think I am doing some mistake
ok perfect
now you can write here
console.log and then error
basically I have copied and pasted this error
this is it
you have to write this much code
and whatever data you have filled
it will be stored in your database
test time has come
I will delete this part
because I don't need it
ok
you can see it is perfectly empty
I will save it
I hope you have understood
each and every line
you can see
error was already there
I will keep it as it is
again error
because console is not doing anything
I will refresh
ok
now
I want to fill complete data
ok
now data is perfect
now I will click on
simply send button
and boom
message user registered successfully
and the best part is
I have created a new user
on my database
user has been created
you can see
I will refresh
and finally
you can see
name, email, phone, work, password
and then confirm password
so guys this is awesome
and whenever data is successful
I feel so good
I feel so good
think about it
I am filling data on postman
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
I am using my mind
Outro