So we want to be able to store a new product and the price should be stored as a 128 bit decimal.
Now to find out how that works, it's always a good idea to dive into the official docs of course.
So here I'm in the nodejs driver 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 we can tell by the new keyword and we could pass some bytes in to create a
decimal
but even more useful is probably this static method which creates a decimal 128
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 mongodb 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 you import it from mongodb 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 mongodb and mongodb 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 catch here after insert one because insert one also returns a so-called promise
so that we can either react to any errors we might be facing
but even in the case of an 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, this line and the line thereafter 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 in this we're done handlers and then in then, I
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
want to close the 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 change the code and now let's go back to our react app and here, you can enter any product
data you want, any price you want,
try adding some decimals and any image url you want,
for example you can use localhost 3100 because that is the temporary host the backend runs on and then
you can pick /images and then any of these image names like product-backpack.jpg and then
here, we can enter a description, does this work, click create product
and I got an error so
let's see what's wrong. Now
the error we got here, string trim 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 toString on the price here.
And now if we save this and we quit our server and restart it because we change the code,
now here we can simply click ok and click create product again
and now this looks better
and now here on the backend, we actually see something that does not look like an error.
Indeed we get back a command result
and you see a lot of information about the operation that was executed.
The host, it sends the data to you
and at the bottom, also the inserted count,
so one document was added 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 other terminal where I'm connected to that same server through 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 through the client, now that we are able to insert data, let's also 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 occurred in case I don't have a more
detailed error message
and here if we did successfully enter it, well then here I will say a product added but I'll not return dummy
ID,
instead I can simply use result inserted ID to return that.
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 send it before we know if it succeeded or not,
so this is better.
With that let's move on to getting data.