1 00:00:00,500 --> 00:00:06,860 well in this video we are going to start to see how to save the data that we are passing from the 2 00:00:06,860 --> 00:00:17,879 front to the back in json format, it is worth before commenting well, write down if we want to put a 3 00:00:17,879 --> 00:00:22,460 longer extension to the routes that we have been creating in the previous video, for example, imagine 4 00:00:22,460 --> 00:00:49,520 that I want to put here, dam 2, okay, dam 2, okay, I could be putting in all the routes that we had, in this case they are only these two, but we are going to do more when we create a new one in the Thunderclient, a new one, a new request, localhost, okay, 5 00:00:49,520 --> 00:00:54,520 So, if I put, for example, register, it will come out registering and such. 6 00:00:54,520 --> 00:01:00,520 Well, you have the option of, instead of putting this extension in all routes, 7 00:01:00,520 --> 00:01:06,519 here I would have missed the slash, okay, instead of doing it here, 8 00:01:06,519 --> 00:01:18,519 as we have all routes configured in router and router we are importing it from appjs, 9 00:01:18,519 --> 00:01:28,420 in the out road variable we can take and put it in the extension here it is worth in the app 10 00:01:28,420 --> 00:01:37,280 use out road because all the links are saved there and whenever you want to make that extension 11 00:01:37,280 --> 00:01:50,359 to all the lines and I here I put dan 2 as it is worth because this is going to work for me exactly 12 00:01:50,359 --> 00:02:01,159 the same by registering and if we put the login because it is the same, it is good because when I want to 13 00:02:01,159 --> 00:02:11,240 do something generic I can do it from the pp js ok and now we are going to work on our 14 00:02:11,240 --> 00:02:18,479 json ok then the json that we send from the front to the back in this case you already know that our 15 00:02:18,479 --> 00:02:25,800 front we are simulating it with the thunder client ok then until now what we did was 16 00:02:25,800 --> 00:02:33,000 answer a registering or logging in, which is what appears to us here in the part of the response 17 00:02:33,000 --> 00:02:45,800 logging in because I am in login or in the register registering is worth then what I was telling you 18 00:02:45,800 --> 00:02:54,379 the other day in the network is the data that we are going to send from the backend to the front and in the 19 00:02:54,379 --> 00:03:01,400 req request is the information that they are sending us from the front to the back in ok then we have 20 00:03:03,699 --> 00:03:11,979 we are going to do different tests so that you see this I am going to leave this for now we are going to leave it like 21 00:03:11,979 --> 00:03:26,000 this ok then we can make a console and that it prints what I am sending from 22 00:03:27,479 --> 00:03:37,539 the front in it in the back then I put the console I am going to print in the terminal 23 00:03:37,539 --> 00:03:48,240 what I am doing if I come to my client to the client I come to the body I see that I can 24 00:03:48,240 --> 00:03:57,240 use different formats of data in this case I am going to use the json and the json I put a 25 00:03:57,240 --> 00:04:08,800 key I put the key that I want to put in quotes is worth for example name and we put the 26 00:04:08,800 --> 00:04:15,639 value, for example, Alice, okay, if I put more fields, for example, 27 00:04:17,399 --> 00:04:31,920 it would be worth putting this comma here, well, I don't care, I'm going to put it like this, 28 00:04:31,920 --> 00:04:38,279 Villablanca in the last key value, I don't put any comma, okay, I'm sending from the 29 00:04:38,279 --> 00:04:47,519 body of the client is worth these two this document with these two key values alicia villablanca 30 00:04:47,519 --> 00:05:00,899 enemy the surname is worth if we come now to the controller of the register route it has to first 31 00:05:00,899 --> 00:05:09,500 print us down here if everything is well done those data json is worth and then registering 32 00:05:09,500 --> 00:05:15,879 then if we come and we give the send we see that if it puts us registering but here we 33 00:05:15,879 --> 00:05:23,319 appears an indefinite but it does not put it as an error it only tells you that those data that you are 34 00:05:23,319 --> 00:05:29,420 sending does not know them ok then what do I have to do come to the pp js and you have to 35 00:05:29,420 --> 00:05:36,800 tell express that the format of data that you are going to use is the json then that here 36 00:05:38,600 --> 00:05:49,079 after lifting the service I am going to put the app.yus we are going to put express.json 37 00:05:49,079 --> 00:05:57,139 ok and now if we go back to our client and send it 38 00:06:00,899 --> 00:06:08,279 you see that the name appears to me alicia the surname villablanca ok then well this 39 00:06:08,279 --> 00:06:13,879 for now has done it for me well now we are going to continue playing with this we are going to put 40 00:06:13,879 --> 00:06:23,540 example in the controller I am going to take it and I am going to say it well I am going to create some variables between 41 00:06:23,540 --> 00:06:38,480 keys and what is going to be we have the name and the username ok well we are going to put a variable that is 42 00:06:38,480 --> 00:06:47,839 name and another one that is the south of these variables I want you to take them from the information that you are 43 00:06:47,839 --> 00:06:53,980 sending us, it is worth then I put here the same and I am going to put the reg point here, 44 00:06:56,079 --> 00:07:04,639 okay, and here I want you to put me that you print me that you draw me the name or and the south 45 00:07:04,639 --> 00:07:15,680 in the position in which I put these variables, it does not matter, it is not necessary to put it in the 46 00:07:15,680 --> 00:07:24,040 same order, it is worth if I now launch you see that it puts me alicia and villablanca, it is not doing it 47 00:07:24,040 --> 00:07:31,879 well, it is worth then what happens if instead of putting the same fields that I am putting in the keys 48 00:07:31,879 --> 00:07:37,759 pongo otros que me va a dar un error porque al final lo que estamos haciendo para la imaginación 49 00:07:37,759 --> 00:07:46,740 que pongo ahí pues eso en dos en lo que estoy haciendo es mapear ese jota son que estoy 50 00:07:46,740 --> 00:07:54,360 recibiendo en el rey punto body a estas variables vale y necesito que me meta en cada una de estas 51 00:07:54,360 --> 00:08:02,339 keys the value that is associated if I put in two I do not understand I do not find that key and that 52 00:08:02,339 --> 00:08:10,800 is going to give us an error that is going to tell me that it is undefined then this so that it is clear 53 00:08:10,800 --> 00:08:20,379 that always the same name is good then we would have our we are receiving the json from the 54 00:08:20,379 --> 00:08:24,519 from we are printing it well 55 00:08:24,819 --> 00:08:32,720 ok if instead of printing it by terminal what I want is that I teach it 56 00:08:32,720 --> 00:08:35,299 by 57 00:08:41,080 --> 00:08:46,620 in the in the as response is worth that we only send that data to the client 58 00:08:46,620 --> 00:08:50,740 then it would be the same as we have done the reset that appears here 59 00:08:50,740 --> 00:08:56,080 registering I now what I want is for the json to appear here instead of in the terminal it is worth 60 00:08:56,080 --> 00:09:06,399 then to do that I do not use the resen I use the res.json it is worth and in parentheses that json that 61 00:09:06,399 --> 00:09:15,899 we want to send in this case because it is the same as the body is worth point and comma and then if we 62 00:09:15,899 --> 00:09:23,399 ejecutamos ahora veis que me está devolviendo el cliente está recibiendo ese jota son así que hemos 63 00:09:23,399 --> 00:09:29,159 enviado este jota son desde el cliente al servidor y luego el servidor me ha vuelto a enviar es el 64 00:09:29,159 --> 00:09:36,080 mismo jota son pero ya sé que estoy enviando y recibiendo un jota son vale seguimos 65 00:09:36,080 --> 00:09:49,740 I what I want now is to pass those data through the model the mongoose skin and then converted 66 00:09:49,740 --> 00:10:01,399 into a model is worth for me to check all these all these options is worth this in the previous video 67 00:10:01,399 --> 00:10:08,600 had it and without the d I had written it wrong, then in the request you have to put a d 68 00:10:10,019 --> 00:10:16,799 that I have seen this before and I have changed it here but you have to change it 69 00:10:16,799 --> 00:10:25,480 you too and in the bus as every time I generate or receive a document because some 70 00:10:25,480 --> 00:10:31,000 instance of that document is worth we are going to put a new one that is better worth then we put the new 71 00:10:31,000 --> 00:10:40,860 y la de vale entonces cómo voy a trabajar con esto vale entonces lo primero que le vamos a 72 00:10:40,860 --> 00:10:49,919 decir es que esos valores que estamos recogiendo en el controller vamos a hacer primero bien la 73 00:10:49,919 --> 00:11:02,789 consulta sería el sería el username vale no no me importa el orden en el que lo pongáis a cuidar 74 00:11:02,789 --> 00:11:17,029 of that we are going to put here because we had also said the email and the password is worth and with the 75 00:11:17,029 --> 00:11:23,269 same names that we have put here is worth then username this 76 00:11:23,269 --> 00:11:34,149 e-mail to put it mario 77 00:11:36,429 --> 00:11:38,870 dandos point rg 78 00:11:40,970 --> 00:11:46,309 and the password we are going to put it 79 00:11:47,889 --> 00:11:55,570 1234 for example and since this is the last key value that I am going to pass I do not put the comma 80 00:11:55,570 --> 00:12:02,190 ok it would be ok this if I go to send it shows me the data here it is ok 81 00:12:03,169 --> 00:12:06,470 ok I come to the controller in the controller now 82 00:12:09,049 --> 00:12:16,029 this I cannot put the name and the sub name because those fields right now do not exist 83 00:12:16,029 --> 00:12:25,649 then here the ones that I am going to pass are the user name the email and the password 84 00:12:27,330 --> 00:12:32,309 these values ​​I collect them from the body this I am going to remove it 85 00:12:34,049 --> 00:12:42,990 and now I have to pass this on to a new user then what is the first thing I am going to do 86 00:12:42,990 --> 00:12:48,629 import that model that we had created this user is worth remembering that when I create 87 00:12:48,629 --> 00:12:54,570 a mongoose point model I put the name that you want I put it user and then what 88 00:12:54,570 --> 00:13:00,690 scheme is associated with this model is worth that is all this that we have put above 89 00:13:02,009 --> 00:13:06,370 then in the controllers the first thing we have to do is import 90 00:13:06,370 --> 00:13:38,340 I do not remember if we have it with default, we have it with default, okay, so well, import, user, from, here we are going to put models, user model, point, remember, js, okay, okay, I already have this user, 91 00:13:38,340 --> 00:13:43,419 then now what I want is to work on these three values these three values that I am going to 92 00:13:43,419 --> 00:13:58,980 do I am going to pass it to a new user and I am going to pass the username I am going to pass the email and I am going to 93 00:13:58,980 --> 00:14:13,610 pass the password ok I pass these balls ok then if this is fine now it should work for us 94 00:14:13,610 --> 00:14:21,049 eye I'm still not saving it in the database then let's go since we're going to validate it 95 00:14:21,049 --> 00:14:27,769 ok and mongoose is not going to skip the validation until we try to save it in the database 96 00:14:27,769 --> 00:14:35,129 what I am going to do is once I pass it through the model through that filter I am going to save that variable I 97 00:14:35,129 --> 00:14:40,190 cannot save the new user that as always we have to declare a variable that in this case 98 00:14:40,190 --> 00:14:47,690 because I can call it new user or as you want it is worth as you want and what I am going to 99 00:14:47,690 --> 00:15:14,759 to do here is because no point 6 is worth and we are going to if you want we are going to console point 100 00:15:14,759 --> 00:15:17,679 news 101 00:15:22,049 --> 00:15:23,629 to see how this works for us 102 00:15:25,889 --> 00:15:32,289 then we are first in our json that everything is fine okay 103 00:15:35,009 --> 00:15:43,190 when we are in our when we have created our connection to mango 104 00:15:46,129 --> 00:16:01,309 yes I have told you that although we created the database until we saved a user a document 105 00:16:01,309 --> 00:16:13,230 in the database was not created then if we come we are seeing this if we come to our 106 00:16:13,230 --> 00:16:20,490 mongo express is worth localhost 8081 I have already logged in I already have here the database and I do not have 107 00:16:20,490 --> 00:16:32,049 any data ok then we go back to our visual studio code in the visual studio code if now 108 00:16:32,049 --> 00:16:41,970 we give it to send ok this visualizes me because we have put the console ok I have put this 109 00:16:41,970 --> 00:16:50,070 option I have put it in new users I see that it has generated this document with the key user name 110 00:16:50,070 --> 00:16:57,129 mario key email with the value mario arroba dan 2 point rg the password 1 2 3 4 and then 111 00:16:57,129 --> 00:17:05,130 also mongo puts us this and this identifier is worth that it only does it automatically if 112 00:17:05,130 --> 00:17:14,009 now we come to our our mongo express ok we come 113 00:17:14,009 --> 00:17:19,549 let's see that we are seeing it that if I update 114 00:17:19,549 --> 00:17:27,069 I have already created the database of dandos must be worth if I go to view 115 00:17:27,069 --> 00:17:33,849 I already have mario mario here giving us 1 2 3 4 ok if you click on it 116 00:17:33,849 --> 00:17:44,230 ya os aparece aquí vale voy a volver al visual studio code vale y entonces aquí vamos a empezar 117 00:17:44,230 --> 00:17:53,490 a hacer formas pruebas entonces tenemos nuestra petición vamos a imaginarnos por ejemplo que yo 118 00:17:53,490 --> 00:18:04,119 intento meter si estos campos o sea este jota son sin la clave username vale cuando la clave 119 00:18:04,119 --> 00:18:16,819 username we have said that it is required then if from here we launch we are going to stop this 120 00:18:24,019 --> 00:18:29,819 if I launch it, it tells me, the error jumps and tells me that the username is required, 121 00:18:29,819 --> 00:18:38,000 then we are going to put the username again, we are going to put another name here, 122 00:18:38,000 --> 00:18:48,099 for example silvia the mail I'm going to leave the same and the password but I'm going to leave the same 123 00:18:48,099 --> 00:19:12,250 this if I give it to the center I get an error ok because because I get the error and it tells me that 124 00:19:12,250 --> 00:19:19,210 here it is worth first believe it silvia mario 125 00:19:23,319 --> 00:19:38,400 here you are ok it says the email is duplicated by putting that the email has to be unique by 126 00:19:38,400 --> 00:19:45,539 putting that it is unique we are telling it that the key is ok then this is already giving us the error yes 127 00:19:45,539 --> 00:19:51,240 we come to see 128 00:19:59,460 --> 00:20:09,220 ok if we come to our express and update ok I see that he has not saved me this 129 00:20:09,220 --> 00:20:22,150 jota are if I change it here and I put sylvia I save sense it is worth this 130 00:20:29,500 --> 00:20:34,400 is worth it is going to save it and if now we come 131 00:20:34,400 --> 00:20:49,579 to our mongo express ok and update because if I have changed the email and it will do everything for me 132 00:20:49,579 --> 00:21:02,119 well ok ok let's go back to our visual studio code what else are we going to be doing here for example 133 00:21:02,119 --> 00:21:12,920 when I do when I save the data inside the mongo db because as we did when we made the 134 00:21:12,920 --> 00:21:18,240 connection when we are going to do an operation on a database, yours is always to do it 135 00:21:18,240 --> 00:21:24,500 asynchronously, it is worth because there may be problems then we already know that to do it asynchronously 136 00:21:24,500 --> 00:21:30,079 you have to put the weight and that has to be accompanied always with the async is worth and 137 00:21:30,079 --> 00:21:36,539 además deberíamos controlar el que se pueda producir algún error entonces vamos a hacer 138 00:21:36,539 --> 00:21:45,359 todo eso entonces ya sabemos cómo es aquí le metemos en la zinc aquí le voy a meter en la 139 00:21:45,359 --> 00:22:00,359 weight we are going to put for example here well for example the track at this value we are going to pass it 140 00:22:06,539 --> 00:22:14,859 ok I'm going to tell you this is fine everything then we are going to put it here control x 141 00:22:14,859 --> 00:22:22,799 control v here I am going to tell him we are going to create a variable with 142 00:22:26,019 --> 00:22:33,569 I use for example ok and we are going to put 143 00:22:35,769 --> 00:22:43,150 control x I do not want it at all we are going to pass the res dot j son 144 00:22:43,150 --> 00:22:57,539 users to see if this is going to take us, it is worth that it shows us there everything is fine and here 145 00:22:58,799 --> 00:23:02,799 good because if there is some type of error that it does not show it by the console 146 00:23:02,799 --> 00:23:24,299 error ok this we are going to erase it ok and so we already put it we do it asynchronously we put it 147 00:23:24,299 --> 00:23:30,240 on the web because this is the operation that we want to do it in the background we visualize we 148 00:23:30,240 --> 00:23:35,400 send the front a json with what we have saved with the data that we have saved 149 00:23:36,839 --> 00:23:41,839 y si hay algún tipo de error también lo controlamos y lo visualizamos vale 150 00:23:41,839 --> 00:23:50,900 entonces vamos a poner aquí silvia vamos a poner 151 00:23:52,579 --> 00:24:00,750 y un 234 vale le doy al senf me visualiza el jota son vale me lo 152 00:24:00,750 --> 00:24:10,109 visualiza bien si venimos a nuestra base de datos 153 00:24:10,109 --> 00:24:17,049 to see that we are here if we come to our database and update I already have here anna 154 00:24:17,049 --> 00:24:30,829 worth silvia and mario so everything is working for us well as we are developing it is worth 155 00:24:30,829 --> 00:24:39,750 creating or doing the function is the register variable is worth the arrow function is worth 156 00:24:40,109 --> 00:25:03,549 What else can we do? Well, instead of using the Mongo Express, if you don't want to be with the browser open and such, I like it more, but there is also an extension that is Mongo, Mongo, this MongoDB, okay, that if you install it, 157 00:25:03,549 --> 00:25:11,710 I am going to appear here the sheet of this of the monkey and then here I tell it to connect and 158 00:25:13,990 --> 00:25:16,150 because we are going to tell it to put 159 00:25:18,549 --> 00:25:29,349 must be two points and the server where we have the monkey is valid in execution that is localhost 160 00:25:29,349 --> 00:25:37,829 ok then here if you see we already have we have already made the connection we already have here our 161 00:25:37,829 --> 00:25:46,369 database giving us we have the different documents ok and in each of those documents and 162 00:25:46,369 --> 00:25:51,089 click on it you also have them ok and it is also a way to see it from the 163 00:25:51,089 --> 00:25:58,670 visual studio code itself ok what else can we do we can put a time a time stamp that is also 164 00:25:58,670 --> 00:26:04,609 usually use a lot within these values and also that it does not do it automatically, it is worth if I tell you 165 00:26:08,849 --> 00:26:14,509 what I told you the other day, it is worth that if you have to use the technical documentation then if 166 00:26:14,509 --> 00:26:26,920 we come here it is worth the documentation of mongoose here you have different options it is worth 167 00:26:26,920 --> 00:26:36,480 pues el máximo o el mínimo número de caracteres que queréis en cada uno de los campos que acompañan 168 00:26:36,480 --> 00:26:40,400 a la clave vale con cada uno de esos valores cuando estamos trabajando con string cuando 169 00:26:40,400 --> 00:26:46,740 estamos trabajando con números con date vale entonces que insisto en que aquí tenéis toda 170 00:26:46,740 --> 00:26:56,539 esta información que la podríamos ir utilizando e y que os tenéis que acostumbrar a utilizarlo 171 00:26:56,920 --> 00:27:03,279 ok then let's go again to our code and let's put then let's do 172 00:27:04,980 --> 00:27:11,319 this I'm going to close this I'm going to close this I'm going to close this I'm going to close 173 00:27:13,680 --> 00:27:19,140 in the user model ok we are going to use the model and here I am going to do another section then 174 00:27:19,140 --> 00:27:27,349 as we are going to do another section we are going to pass here a comma and another key ok and I create 175 00:27:27,349 --> 00:27:34,450 another section that I am going to put in this section that I am going to say because it automatically saves me 176 00:27:34,450 --> 00:27:41,809 the time the date at which this document has been created, so I am going to put it 177 00:27:41,809 --> 00:27:54,039 time stamp, you have it, they are two points and we are going to tell it to be true, it is ready 178 00:27:54,039 --> 00:28:02,279 and then this when we are going to generate a new document that this is going to be 179 00:28:05,849 --> 00:28:22,170 jacinto and 1234 you see how now it is they have automatically come out these two fields the 180 00:28:22,170 --> 00:28:29,789 created one with the date and the update is worth with the same date if I did some kind of 181 00:28:29,789 --> 00:28:36,549 update on this data because I was going to modify it too, it is worth and it is interesting if we come now 182 00:28:38,630 --> 00:28:41,009 to express 183 00:28:43,269 --> 00:28:52,990 update ok I already have here to jacinto and I have the fields of these dates that we have left them before 184 00:28:52,990 --> 00:29:00,549 without putting it in, that is why it is also important to have that scheme and that mongoose model 185 00:29:00,549 --> 00:29:12,789 so that all the data has its values and here we leave this video a greeting see you later