嵌套控制器.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按钮时,我无法进行任何更改
2条答案
按热度按时间odopli941#
既然patch方法在postman中运行良好,服务器也运行良好,这里有一个前端调试技巧
硬编码URL ID并将param替换为硬编码值:
r1zk6ea12#
现在它正常工作了