So let's continue with deleting products
maybe. For this I got my product delete handler in here and I'll later remove all that code down there,
for now what I'll do is I'll again get access to mongodb with the same code I used in fetch
data
and actually I can also copy that code here where I reach out to the products collection because that
will be the same
and yes you could outsource this into a separate function therefore. Here,
I will then call delete one because I want to delete one product
and of course I need to pass my filter just as you always did it, just as you learned in this course.
So I want to delete
one product which I identify by ID.
So I will delete one product where the ID is equal to the Product ID
I get as an argument here. Now then I can chain then and catch and I can basically copy the code I have
down there,
so I'll copy the function I had for catch and move that into my catch block up there and well basically
I can also use the entire then block and reuse it up there.
So with that, this should delete my items,
I can now get rid of that old code where we used axios and only have that new code where I used
mongodb stitch.
If we now save that, the page gets reloaded and let's now try to delete the working MacBook.
If I click on delete here,
you see I get back a deleted count of zero and
do you have an idea of what's wrong here?
Well the problem is that I have an ID which is a string and I'm looking for ID in my database where
this equals the string I get here
but the ID in the database is an object ID.
So we need to create an object id and we can do that in the browser with the help of a special package
which we can install with npm install --save, whoops, --save bson,
this is a package that allows us to create bson types so these special types mongodb works
with in the browser and we can create an objectid object in the browser therefore.
So with that installed, we can get rid of axios for one,
we can now import bson from that bson package here, we can use bson down there in our delete
handler where I am looking for the product ID,
we can use bson. and then simply object ID
and we have to instantiate this.
Now with that, we're creating a new objectId here and we're passing our string there.
Now if we save this, that won't suffice, we also need to restart react, the react server because we installed a new
package.
So now once this reloaded here, we can try deleting again
and now you see it deleted one product.
So now this works thanks to us using bson.