Search code examples
reactjsaxiosnestjspatch

How to make a PATCH request in ReactJS ? (with Nestjs)


nestjs controller.ts

   @Patch(':id')
    async updateProduct(
      @Param('id') addrId: string,
      @Body('billingAddr') addrBilling: boolean,
      @Body('shippingAddr') addrShipping: boolean,
    ) {
      await this.addrService.updateProduct(addrId, addrBilling, addrShipping);
      return null;
    }

nestjs service.ts

 async updateProduct(
    addressId: string,
    addrBilling: boolean,
    addrShipping: boolean,

  ) {
    const updatedProduct = await this.findAddress(addressId);
    if (addrBilling) {
      updatedProduct.billingAddr = addrBilling;
    }
    if (addrShipping) {
      updatedProduct.shippingAddr = addrShipping;
    }
    updatedProduct.save();
  }

there is no problem here. I can patch in localhost:8000/address/addressid in postman and change billingAddr to true or false.the backend is working properly. how can i call react with axios?

page.js

    const ChangeBillingAddress = async (param,param2) => {
        try {
            await authService.setBilling(param,param2).then(
                () => {
                    window.location.reload();
                },
                (error) => {
                    console.log(error);
                }
            );
        }
        catch (err) {
            console.log(err);
        }
    }
return....
     <Button size='sm' variant={data.billingAddr === true ? ("outline-secondary") : ("info")} onClick={() => ChangeBillingAddress (data._id,data.billingAddr)}>

auth.service.js

const setBilling = async (param,param2) => {
   let adressid = `${param}`;
   const url = `http://localhost:8001/address/`+ adressid ;
   return axios.patch(url,param, param2).then((response) => {
      if (response.data.token) {
         localStorage.setItem("user", JSON.stringify(response.data));
      }
      return response.data;
   })
}

I have to make sure the parameters are the billlingddress field and change it to true. I can't make any changes when react button click


Solution

  • now it worked correctly

       @Patch('/:id')
        async updateProduct(
          @Param('id') addrId: string,
          @Body('billingAddr') addrBilling: boolean,
        ) {
          await this.addrService.updateProduct(addrId, addrBilling);
          return null;
        }
    
    
    const ChangeBillingAddress = async (param) => {
    
        try {
            await authService.setBilling(param,true).then(
                () => {
                    window.location.reload();
                },
                (error) => {
                    console.log(error);
                }
            );
        }
        catch (err) {
            console.log(err);
        }
    }
    
    
    const setBilling= async (param,param2) => {
       let id = `${param}`;
       const url = `http://localhost:8001/address/`+ id;
       return axios.patch(url,{billingAddr: param2}).then((response) => {
          if (response.data.token) {
             localStorage.setItem("user", JSON.stringify(response.data));
          }
          return response.data;
       })
    }