How to correctly send a patch request in frontend and backend in node environment

Viewed 30

Hello, I am trying to update a resource using patch method. I am completely unable to do so. I am using only node and no react.

My Mongo Db Schema My Mongo dB schema

I have to update the desc field. my backend patch request is working. The code for patch request is My Patch request at backend

At postman it is working fine

Postman patch request

you can see the record in the dB is updated

mango dB record updated

I need to update it through a form having a text area.

My Front End form

I have attached a function in my frontend JavaScript to the submit button for creating a fetch request

My Front end js code

Below is my Form in html.

My Html form

Here is how the button is linked with the function

link between the form submit button and fetch function

This is the error I am getting

The Error

Here is my frontend console

console

My Fetch Request for Patch Javascript Codes

await fetch(window.location.href, {
        method: 'PATCH',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
        credentials: 'include',  
        body: JSON.stringify({
          desc: abc,
        })
        
      })
        .then((response) =>  {
            console.log(response);
            return response;
        })
        .then((data) => {
            console.log(data);
        })
  };
0 Answers
Related