So we want to be able to store a new product and the prize should be stored as a 128 decimal.
Now to find out how it works it's always a good idea to dive into the official docs of course.
So here I'm in the no chest drive or docs.
And one way of finding out how this works is to go into the driver API and look for something related
to decimal.
And indeed here is a decimal 128 object.
Now this is a constructor as you can tell by new word and we could pass some bytes in to create that
decimal.
But even more useful is probably just a static method which creates a decimal one hundred twenty eight
instance from a string representation.
And that's exactly what I want.
So now we just have to go back and import that object from long OODB which is why I now import this
separately because now I can use my decimal or can create a constant named decimal 128.
That name is up to you though.
And imported from Mungiki be here decimal 128.
So now I can use this constant as a reference to that decimal 128 object.
And here instead of using the native javascript function parse float I can use decimal 128 from string.
And with that we hopefully can extract the price and convert it to a decimal 128 object that will then
be sent to Mongo D.B and Mangu to be will know how to store it.
That was a lot of talking but with that we just have to do one more thing.
We can call then and catchier after is a good one because instead one also returns a so-called promise
so that we can ever react to any errors we might be facing.
But even in the case of error I want to close the connection but I want to do that inside of catch so
that I don't close it too early because the way javascript works line and aligned her offer will be
executed right after each other.
Javascript doesn't wait for this operation to finish.
So anything that relies on it being finished should go Indies were done handlers and then and then I
also get back to a result which I can lock to the consulate and can see what I get back and then I also
want to close to client.
So with that I now restructured my code.
I'm now connecting inside of that post route and I'm sending data to the database to the products collection
and then hopefully we have some working code here.
So if I now save this save all files and I go back to that window where I ran NPM run start server we
can run this again.
Because we changed the code and now let's go back to our re-act app and here you can enter any product
data you want any price you want.
Try adding some decimals and any image you or all you want.
For example you can use localhost 4100 because dad is a temporary host the back end runs on and then
you can pick slash it meant that any of these image names like product Dasch backpack to JPAC and and
here we can enter that description just as work click Create product.
And I got an error.
Let's see what's wrong now.
The error we got here string trimmer is not a function is related to the decimal 128 from string method.
And the problem is that this price here is not a string by default.
So we first of all can convert it to a string by calling two string on the pricier.
And now if we saved us and we quit our server and restart it because we changed to code.
Now here we can simply click OK and click trade product again.
And now it just looks better.
And now here on the backend we actually see something that does not look like an error.
Indeed we get back command result.
And you see a lot of information about the operation that was executed.
The host sends the data to you.
And at the bottom also the inserted count.
So one document was edit and the ID not an object ID because in javascript object ID is not an object
that would exist but basically the string which is wrapped by object ID.
And if I go to my Alber turtle where I'm connected to that same server from the shell if I show collections
we do see the products collection now.
And here if I look into it with find pretty we do see that one product I just added.
We see the price is a number decimal we see the image link here and we see the object id and this part
here in the middle is just what we got back here.
So this is now a working insert from the client now that we are able to insert data lets all the fetch
data in the next lecture.
Now before we are done inserting though there is one thing I want to change when we send a response
here.
I want to send that inside of the then block or of the catch block here in the catch block I'll send
an error response where I maybe say something like an error or occurred in case I don't have a more
detailed error message.
And here if we did successfully enter it well then I will say a product at it but not returned dummy
ID.
Instead I can simply use result in inserted ID to return debt.
And this is now better because we should return the response once we are done with this asynchronous
task and not outside of our promise because then we actually sent a response before we are done with
that and therefore we sent it before we know if it succeeded or not.
So does this matter.
With that lets move on to getting data.