So it's time to add the functionality to add a new product
and we do add this in added product. There
we have the code to first of all get an existing product for or the details for an existing product, we'll
care about this later but we want to care about this case here where we check where we're editing or
adding and then we execute different requests.
Well that is in the end where I want to pick up and here, if we're not editing,
so if I would create a new product, this is the part where I will now add my code. Again I will
simply import these two imports into added products.js
because we'll need them here,
I'll leave axios in there for now
so that the code doesn't break immediately
and then here in else, of course I will use that same code I used before where I get access to mongodb
to the products collection,
so I'll copy that into here too
and then there, I want to insert one new element and that one new element of course is my product data
here.
So I will pass product data into here,
however now I need to make sure that I do convert the types of product data, so the price
for example should be a decimal right
and here I can use bson.decimal 128, not with a new in front of it, just like this
and then there is a from string method.
So I can pass a string in there, for this I have to convert this to a string first though or make sure
this is a string,
it should be by default but to be really sure and then simply create such a decimal 128 object based
on that string.
With that added, here I got the logic to insert one product
and now this in the end is my new request or not const, just request, I'm initializing this variable up
here,
here I'm assigning a value, request will hold a promise because insert one returns a promise
and here I then just handle that promise,
you can also output the results here to see what we get back.
With that,
if I now save that, this page should reload
and now let's add some content here.
Let's also grab some image of your choice,
so here I'll just paste in the url of a burger, a tasty burger
and then click create product and I actually get an error, unexpected token add position something.
Now what could be the reason for this error?
It has something to do with well something we covered earlier regarding to how we work with users and
permissions. Well we're simply not allowed to write data,
that is why we have a 403 error there which is the code for not authorised.
We have to go into our stitch rules again and keep in mind, we're only able to read products and that
is that finegrained control I meant,
now we have to unlock writing too so that we are able to save that.
And with that change, we can go back,
you don't even need to reload the page and you can just click create and now it works
and now you see the burger here.
So that shows you again this finegrained permission set up that stitch uses.