Fetching the create API in the view

In the frontend, to make a request to this create API, we will set up a fetch method on the client-side to make a POST request to the API route and pass it the multipart form data containing details of the new auction in the body. This fetch method will be defined as follows.

mern-marketplace/client/auction/api-auction.js:

const create = (params, credentials, auction) => {
return fetch('/api/auctions/by/'+ params.userId, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + credentials.t
},
body: auction
})
.then((response) => {
return response.json()
}).catch((err) => console.log(err))
}

The response that's received from the server will be returned to the component calling this fetch method. We will use this method in the new auction form view to send the user-entered auction details to the backend and create a new auction in the database. In the next section, we will implement this new auction form view in a React component.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset