1 00:00:02,420 --> 00:00:05,640 So it's time to add the functionality to add a new product 2 00:00:05,640 --> 00:00:08,520 and we do add this in added product. There 3 00:00:08,640 --> 00:00:16,320 we have the code to first of all get an existing product for or the details for an existing product, we'll 4 00:00:16,440 --> 00:00:22,500 care about this later but we want to care about this case here where we check where we're editing or 5 00:00:22,770 --> 00:00:25,880 adding and then we execute different requests. 6 00:00:25,890 --> 00:00:29,700 Well that is in the end where I want to pick up and here, if we're not editing, 7 00:00:29,700 --> 00:00:37,110 so if I would create a new product, this is the part where I will now add my code. Again I will 8 00:00:37,350 --> 00:00:42,400 simply import these two imports into added products.js 9 00:00:42,420 --> 00:00:44,140 because we'll need them here, 10 00:00:44,270 --> 00:00:45,970 I'll leave axios in there for now 11 00:00:46,010 --> 00:00:48,380 so that the code doesn't break immediately 12 00:00:48,620 --> 00:00:54,170 and then here in else, of course I will use that same code I used before where I get access to mongodb 13 00:00:54,170 --> 00:00:55,840 to the products collection, 14 00:00:55,910 --> 00:00:58,330 so I'll copy that into here too 15 00:00:58,790 --> 00:01:05,440 and then there, I want to insert one new element and that one new element of course is my product data 16 00:01:05,440 --> 00:01:05,950 here. 17 00:01:06,710 --> 00:01:10,510 So I will pass product data into here, 18 00:01:10,520 --> 00:01:17,120 however now I need to make sure that I do convert the types of product data, so the price 19 00:01:17,120 --> 00:01:19,240 for example should be a decimal right 20 00:01:19,310 --> 00:01:26,100 and here I can use bson.decimal 128, not with a new in front of it, just like this 21 00:01:26,120 --> 00:01:28,170 and then there is a from string method. 22 00:01:28,220 --> 00:01:34,550 So I can pass a string in there, for this I have to convert this to a string first though or make sure 23 00:01:34,550 --> 00:01:35,460 this is a string, 24 00:01:35,540 --> 00:01:42,140 it should be by default but to be really sure and then simply create such a decimal 128 object based 25 00:01:42,140 --> 00:01:43,440 on that string. 26 00:01:44,000 --> 00:01:48,630 With that added, here I got the logic to insert one product 27 00:01:48,770 --> 00:01:56,210 and now this in the end is my new request or not const, just request, I'm initializing this variable up 28 00:01:56,210 --> 00:01:56,700 here, 29 00:01:56,720 --> 00:02:02,090 here I'm assigning a value, request will hold a promise because insert one returns a promise 30 00:02:02,150 --> 00:02:04,300 and here I then just handle that promise, 31 00:02:05,690 --> 00:02:09,740 you can also output the results here to see what we get back. 32 00:02:12,780 --> 00:02:13,370 With that, 33 00:02:13,470 --> 00:02:16,350 if I now save that, this page should reload 34 00:02:16,530 --> 00:02:20,570 and now let's add some content here. 35 00:02:20,580 --> 00:02:23,650 Let's also grab some image of your choice, 36 00:02:23,970 --> 00:02:29,510 so here I'll just paste in the url of a burger, a tasty burger 37 00:02:29,670 --> 00:02:37,880 and then click create product and I actually get an error, unexpected token add position something. 38 00:02:37,890 --> 00:02:40,580 Now what could be the reason for this error? 39 00:02:40,580 --> 00:02:46,310 It has something to do with well something we covered earlier regarding to how we work with users and 40 00:02:46,310 --> 00:02:50,660 permissions. Well we're simply not allowed to write data, 41 00:02:50,660 --> 00:02:55,580 that is why we have a 403 error there which is the code for not authorised. 42 00:02:56,090 --> 00:03:04,010 We have to go into our stitch rules again and keep in mind, we're only able to read products and that 43 00:03:04,010 --> 00:03:05,780 is that finegrained control I meant, 44 00:03:05,840 --> 00:03:11,950 now we have to unlock writing too so that we are able to save that. 45 00:03:12,110 --> 00:03:13,980 And with that change, we can go back, 46 00:03:14,000 --> 00:03:19,580 you don't even need to reload the page and you can just click create and now it works 47 00:03:19,580 --> 00:03:21,530 and now you see the burger here. 48 00:03:21,620 --> 00:03:26,900 So that shows you again this finegrained permission set up that stitch uses.