Fetching the create post API in the view

We will update api-post.js by adding a create method to make a fetch call to the create API. The create fetch method will look as follows.

mern-social/client/post/api-post.js:

const create = async (params, credentials, post) => {
try {
let response = await fetch('/api/posts/new/'+ params.userId, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + credentials.t
},
body: post
})
return await response.json()
} catch(err) {
console.log(err)
}
}

This method, like the user edit fetch method, will send a multipart form submission using a FormData object that will contain the text field and the image file. Finally, we are ready to integrate this create new post feature in the backend with a frontend component that will allow users to compose a post and submit it to the backend.

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

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