1 00:00:02,150 --> 00:00:09,540 So we want to be able to store a new product and the price should be stored as a 128 bit decimal. 2 00:00:09,920 --> 00:00:15,370 Now to find out how that works, it's always a good idea to dive into the official docs of course. 3 00:00:15,800 --> 00:00:18,420 So here I'm in the nodejs driver docs 4 00:00:18,680 --> 00:00:25,490 and one way of finding out how this works is to go into the driver API and look for something related 5 00:00:25,490 --> 00:00:26,410 to decimal 6 00:00:26,570 --> 00:00:30,240 and indeed here is a decimal 128 object. 7 00:00:30,260 --> 00:00:36,380 Now this is a constructor as we can tell by the new keyword and we could pass some bytes in to create a 8 00:00:36,380 --> 00:00:37,150 decimal 9 00:00:37,220 --> 00:00:42,650 but even more useful is probably this static method which creates a decimal 128 10 00:00:42,650 --> 00:00:44,800 instance from a string representation 11 00:00:44,840 --> 00:00:47,230 and that's exactly what I want. 12 00:00:47,780 --> 00:00:53,990 So now we just have to go back and import that object from mongodb which is why I now import this 13 00:00:53,990 --> 00:01:01,150 separately because now I can use my decimal or can create a constant named decimal 128, 14 00:01:01,280 --> 00:01:03,150 that name is up to you though 15 00:01:03,590 --> 00:01:08,480 and you import it from mongodb here, decimal 128. 16 00:01:08,830 --> 00:01:14,460 So now I can use this constant as a reference to that decimal 128 object 17 00:01:14,780 --> 00:01:23,850 and here instead of using the native javascript function parse float, I can use decimal 128 from string 18 00:01:23,980 --> 00:01:30,380 and with that, we hopefully can extract the price and convert it to a decimal 128 object, that will then 19 00:01:30,380 --> 00:01:35,470 be sent to mongodb and mongodb will know how to store it. 20 00:01:35,480 --> 00:01:40,150 That was a lot of talking but with that, we just have to do one more thing, 21 00:01:40,370 --> 00:01:47,480 we can call then and catch here after insert one because insert one also returns a so-called promise 22 00:01:48,110 --> 00:01:54,200 so that we can either react to any errors we might be facing 23 00:01:54,260 --> 00:01:59,580 but even in the case of an error, I want to close the connection but I want to do that inside of catch so 24 00:01:59,610 --> 00:02:05,180 that I don't close it too early because the way javascript works, this line and the line thereafter will be 25 00:02:05,180 --> 00:02:06,890 executed right after each other, 26 00:02:06,920 --> 00:02:09,590 javascript doesn't wait for this operation to finish, 27 00:02:09,710 --> 00:02:16,340 so anything that relies on it being finished should go in this we're done handlers and then in then, I 28 00:02:16,340 --> 00:02:22,550 also get back the result which I can log to the console so that we can see what I get back and then I also 29 00:02:22,550 --> 00:02:24,460 want to close the client. 30 00:02:24,920 --> 00:02:28,090 So with that, I now restructured my code, 31 00:02:28,100 --> 00:02:34,370 I'm now connecting inside of that post route and I'm sending data to the database, to the products collection 32 00:02:34,670 --> 00:02:38,210 and then hopefully, we have some working code here. 33 00:02:38,450 --> 00:02:48,420 So if I now save this, save all files and I go back to that window where I ran npm run start server, we 34 00:02:48,420 --> 00:02:49,730 can run this again 35 00:02:50,520 --> 00:02:59,680 because we change the code and now let's go back to our react app and here, you can enter any product 36 00:02:59,680 --> 00:03:02,820 data you want, any price you want, 37 00:03:02,820 --> 00:03:07,680 try adding some decimals and any image url you want, 38 00:03:07,680 --> 00:03:18,150 for example you can use localhost 3100 because that is the temporary host the backend runs on and then 39 00:03:18,240 --> 00:03:26,450 you can pick /images and then any of these image names like product-backpack.jpg and then 40 00:03:26,450 --> 00:03:32,010 here, we can enter a description, does this work, click create product 41 00:03:32,220 --> 00:03:33,550 and I got an error so 42 00:03:33,610 --> 00:03:35,580 let's see what's wrong. Now 43 00:03:35,610 --> 00:03:42,090 the error we got here, string trim is not a function is related to the decimal 128 from string method 44 00:03:42,090 --> 00:03:46,420 and the problem is that this price here is not a string by default, 45 00:03:46,470 --> 00:03:52,670 so we first of all can convert it to a string by calling toString on the price here. 46 00:03:52,800 --> 00:03:59,050 And now if we save this and we quit our server and restart it because we change the code, 47 00:03:59,120 --> 00:04:03,680 now here we can simply click ok and click create product again 48 00:04:03,690 --> 00:04:05,190 and now this looks better 49 00:04:05,550 --> 00:04:10,850 and now here on the backend, we actually see something that does not look like an error. 50 00:04:10,950 --> 00:04:13,920 Indeed we get back a command result 51 00:04:14,400 --> 00:04:19,150 and you see a lot of information about the operation that was executed. 52 00:04:19,650 --> 00:04:21,990 The host, it sends the data to you 53 00:04:22,500 --> 00:04:25,970 and at the bottom, also the inserted count, 54 00:04:25,980 --> 00:04:32,660 so one document was added and the ID. Not an object ID because in javascript, object ID is not an object 55 00:04:32,670 --> 00:04:37,700 that would exist but basically the string which is wrapped by object ID. 56 00:04:37,850 --> 00:04:46,880 And if I go to my other terminal where I'm connected to that same server through the shell, if I show collections, 57 00:04:47,150 --> 00:04:49,180 we do see the products collection now 58 00:04:49,430 --> 00:04:56,350 and here if I look into it with find pretty, we do see that one product I just added, 59 00:04:56,360 --> 00:05:03,470 we see the price is a number decimal, we see the image link here and we see the object id and this part 60 00:05:03,470 --> 00:05:07,770 here in the middle is just what we got back here. 61 00:05:07,790 --> 00:05:15,410 So this is now a working insert through the client, now that we are able to insert data, let's also fetch 62 00:05:15,410 --> 00:05:17,540 data in the next lecture. 63 00:05:17,540 --> 00:05:23,270 Now before we are done inserting though, there is one thing I want to change. When we send a response 64 00:05:23,270 --> 00:05:24,200 here, 65 00:05:24,200 --> 00:05:30,670 I want to send that inside of the then block or of the catch block. Here in the catch block, I'll send 66 00:05:30,670 --> 00:05:37,160 an error response where I maybe say something like an error occurred in case I don't have a more 67 00:05:37,160 --> 00:05:38,490 detailed error message 68 00:05:38,840 --> 00:05:45,290 and here if we did successfully enter it, well then here I will say a product added but I'll not return dummy 69 00:05:45,290 --> 00:05:45,850 ID, 70 00:05:45,950 --> 00:05:50,460 instead I can simply use result inserted ID to return that. 71 00:05:50,690 --> 00:05:55,160 And this is now better because we should return the response once we are done with this asynchronous 72 00:05:55,160 --> 00:06:01,040 task and not outside of our promise because then, we actually sent a response before we are done with 73 00:06:01,040 --> 00:06:04,240 that and therefore we send it before we know if it succeeded or not, 74 00:06:04,310 --> 00:06:05,370 so this is better. 75 00:06:05,480 --> 00:06:07,640 With that let's move on to getting data.