REST API - PUT or UPDATE data into database using Node JS, Mongodb and Postman tutorial

In Web Development

REST API - PUT or UPDATE data into database using Node JS, Mongodb and Postman tutorial - read the full article about Node Js update, Web Development and from United Top Tech on Qualified.One
United Top Tech
Youtube Blogger

hey hello everyone in this video i  will be showing you how to perform put or update operation in your rest api so here  i will be using postman node.js and mongodb to do this so lets get started so currently im having  here a code for all the connection to mongodb the schema of the database and the post operation  over here so i have done a entire video on this all this connection and how to post data so if  you want to watch this video you can just click on this icon or the card that is appearing above you  can watch that video and you can continue with the put operation so if you dont want to watch you  can just continue watching this video its your choice so first let me give you a demo on how  this post operation works and then lets just go and code the update operation first  let me just go to my terminal and type node main dot js so its running  now so if i just now go to postman and its /post i will put uh d  okay i will change these things and put it to three and if i just send were  getting posted and if i just now go to my mongodb refresh ill be getting this value over  here so really simple post operation and now lets jump into our update okay  so first i will just go here and ill put put operation over here so its going to be like  a very very similar code okay but the difference is that here we will be pushing all the data  in the request body itself but in the put or the update operation we will be getting that id  through the params okay through this parameter okay i will also show that to you okay so first  lets begin with the code okay its going to be app dot put okay and next is our url over  here okay so its going to be ill just put slash update slash and next thing as i told you  earlier well be getting the id in the form of a parameter from the url itself so its going to  be colon id so we will be with the help of this id we will be uh updating the particular data okay  and its going to be comma async and the regular thing is going to be request comma response over  here and arrow function and curly bracket okay thats it so next thing we need to have access to  this id value okay but here we use request.body to get the value and here you will be using  request dot params okay so let me just put let my updating id ill just call it as upid  over here equal to request dot params well be having this params which directly corresponds  to this getting fetching value from this url dot id so this id corresponds to this one over  here thats why ive given a colon which means that we can access it through the this params  keyword so its going to be like this so ill just copy this and i will just go to my postman  and if i just go to put okay and instead of slash post well be putting slash update and here  well pass some id lets say it is going to be one and in the json we uh we will be not passing our  id okay because we are getting the id from our url itself so why we need to pass id over here so i  will just remove this we will be passing the name and email which is to be updated and we will be  passing the id of that particular name and email in the parameter okay really simple i guess so  now we need to also have this name and email value right so for that what we will do is just copy  this paste it and instead of this up id i will put update name and instead of this params its  going to be body and for id its going to be name okay so just say copy this paste it over here its  going to be upemail and here i will be putting email okay and the really important thing is that  here im using params and here i am using body so this is quite similar to the post operation  where i have used body as well so that i think the reason is obvious this params corresponds  to this url and this corresponds to the request body so thats why i am fetching the id from the  url and i am getting the name and email from the body so thats the reason why im using params  and body over here so now lets just jump into the main update operation okay so for updating  anything we will be having like two steps okay the first one is going to be we need to find that  particular id we need to first check whether that particular data that were trying to update is  like initially present and the second operation is obvious just update it okay so these are the  two things that everyone does but we can do the two things with the help of a single function  itself the function name is find one and update okay so for that first what you need to have is  you need to have the model name okay so this is the model that i have created over here its mon  model okay so you need to have that model name so just copy your model name and just come inside  this function paste it dot you need to have find one and update so this is the function over  here just click it so this will take like i guess four parameters or four arguments really  you need to be really careful over here so the first argument is going to be id for which we want  to update the data so its going to be just open curly bracket and just pass id colon and up id  okay so uh basically this id corresponds to that value this id okay corresponds to this id and this  up id corresponds to the id that we are passing as the parameter over here okay i hope that made  sense and this is going to be the first parameter and the next parameter is going to contain all  the values that we want to change or update so next thing is going to be again  open curly bracket dollar set colon open curly bracket next thing is going to be  name colon upname comma email colon upemail okay name and email corresponds to that key present  in this database and this upname and upemail is obvious its the data that we are passing from  our postman okay this is going to be our second argument and you need to be careful just come out  of it put a comma over here and were going to put new colon true okay i will explain you this  why we are putting new:true at the end okay now just put a comma over here and open  circular bracket here its going to be error comma data okay really simple if any error  is going to come this error will catch that and this is going to be our data okay so after this  just uh arrow function and just open curly bracket okay so if you cant view this i will what i will  do is i will just okay so this is going to be like a continuation from that you can just also type  it so i think now its perfectly visible over here so now we are just going to have a simple  if condition okay and this function finds and updates okay so its basically performing two  operations so now lets have two conditions okay our first condition is going to be here i  am having data of id one two and three so what if i go to postman and put four okay update  slash four so this four isnt going to be present in our database itself so in that case our data  value will be null okay so for that we will do if data equal to null okay that is our id isnt  present okay we will just put response dot send we can just put nothing found okay and our second  condition is going to be obvious if our id is present and the data is also present in that case  it will automatically update that particular value with the required name and email so in that case  in that case well be putting a else operation and we will put response dot send our data so we will  be viewing this data in the postman it will be coming over here okay so thats the meaning for  that so now lets just run and test it out okay i think we are pretty much done over here lets  run and see it now so im just saving it and im just stopping it now and just restarting it again  so its on port 3000 i will just go to my postman and first i will check what are the ids that i  present so 4 is not present like as i told you earlier so now ill just put update 4 and let  my name be something ok i will just put 1111 will not be my name and it be my email okay  something so im putting four so we need to get uh response dot send nothing found should be  present coming inside the console over here so i will just send it now so as you can see im  getting nothing found which is absolutely right and we are having this b okay okay i will do  a itself so we are having this id 1 and having name as a , email as [email protected] so i will now just  change this values okay so for that really simple just go over here and put one i will just pass  this one itself for all so i will just send it and im getting this as a response over here the  reason is this line is being sent it automatically updates it and sends the response the data  over here and we are getting this over in our postman so now if i just go to my mongodb  it will be the same okay but if i just refresh this watch over here just refresh it ill  be having one one one over here thats it we have successfully performed our update operation  another thing as uh i wanted to mention is that i told you i will explain you why we are putting  this new: true right so if i just put new as false im putting this new as false okay and im  just saving it ill just stop it so we got the updated data right we got the updated data  in our postman over here so it was earlier a as the name and a@abc a dot dot com something  which with the id one but we are getting the updated value in the console of our postman so  what if we wanted the existing data over here so in place of one one one we wanted  a and we wanted [email protected] the existing value that was present before our updation  operation so in that case we will be putting new as false over here so now let me just restart  it node main.js so same thing its running now so now as you can see here we are getting having this  value and i will just update the same thing i will put one as the id and instead of all ones i will  put all 2s over here all 2s over here as well and now if i just send it i will be getting this same  data over here so if i just send it now so i am getting the same data over here but in my mongodb  database if i just refresh it it will be updated but im still getting the old data so i hope that  made sense to you if you put new equal to true over here we will be getting the updated data as  the response over here and if you put new equal to false over here we will be getting the data before  updation that is the old data or the previous data okay i hope that that was clear to you and now  let me just put it uh to true okay ill just put it to true and i will just stop this and  the final thing that we are left with we did it we didnt even use this error okay its really  important to do error handling so if i just put if just put if error and i will just have response  dot send i will just put error okay and i will have a else block over here and just close it and  put this entire if else block within that else block okay so if you are getting error we will be  getting only this as a response and this wont be even executed so now i hope this works i will  just save it and i will just run it again and i will just go over here so ill just  put the same thing ill put three over here uh three over here so im putting name and email  you can just give a proper name and im just going with the numbers between that so if we just run it  now so im getting the updated one and so how to get an error okay so i need to get response as  error over here so let me try this way instead of one uh i will put a okay so ill just send it  now so im getting an error okay so this ERROR is being sent because we have given the id as  a number i guess so yes id is a number so we cant pass a character or a string over there so  thats it i guess we have successfully covered put operation by using nodejs and mongodb and  i hope you would have found this video useful i also run many other videos in c c plus plus java  python sql queries as well as 3d animation using blender so do check other videos of my channel  subscribe and support me thanks for watching

United Top Tech: REST API - PUT or UPDATE data into database using Node JS, Mongodb and Postman tutorial - Web Development