axios 如何在ReactJS?(带Nestjs)中发出PATCH请求

ycggw6v2  于 2022-11-29  发布在  iOS
关注(0)|答案(2)|浏览(170)

嵌套控制器.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服务.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();
  }

这里没有问题。我可以在postman中修补localhost:8000/address/addressid并将billingAddr更改为true或false。后端工作正常。我如何调用axios的reaction?
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;
   })
}

我必须确保参数是billlingddress字段,并将其更改为true。当单击react按钮时,我无法进行任何更改

odopli94

odopli941#

既然patch方法在postman中运行良好,服务器也运行良好,这里有一个前端调试技巧
硬编码URL ID并将param替换为硬编码值:

const setBilling = async (param,param2) => {
   // let adressid = `${param}`;
   const url = `http://localhost:8001/address/123`; // hard code a addressid 

   return axios.patch(url,param, param2).then((response) => { // hard code params too
      console.log(response); // see console result

      if (response.data.token) {
         // localStorage.setItem("user", JSON.stringify(response.data));
      }

      // return response.data;
   })
}
r1zk6ea1

r1zk6ea12#

现在它正常工作了

@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;
   })
}

相关问题