javascript - 为什么我的数组在发布请求后返回未定义?
问题描述
我有一个函数可以将详细信息添加到详细信息数组中,第二个函数可以向服务器发出 post 请求。我第一次运行 addDetail() 然后 addOrder() addDetail() 中的 console.log() 显示一个数组。但是第二次我想在不重新加载页面的情况下运行这两个函数时,addDetail() 中的 console.log() 返回未定义。
在 addOrder() 末尾的 clearOrder() 中,我将数组设置为空: this.order.details=[];
async addDetail() {
let leave = 0;
try {
const response = await axios.get(
`product/products/${this.detailProduct}`
);
const product = response.data.product;
const data = {
product: product.name,
quantity: this.detailQuantity,
aggregateDiscount: this.detailAggregateDiscount,
price: parseFloat(
(
product.finalPrice * this.detailQuantity -
(product.finalPrice *
this.detailQuantity *
this.detailAggregateDiscount) /
100
).toFixed(2)
)
};
this.order.total += data.price;
let num = parseFloat(this.order.total.toFixed(2));
this.order.total = num;
this.order.details.push(data);
**console.log(this.order.details);** // returns undefined the second time
this.detailProduct = "";
this.detailQuantity = 1;
this.detailAggregateDiscount = 0;
} catch (err) {
errorAlertHandler(err, "No encontrado");
}
},
async addOrder() {
const data = {
number: this.totalOrders + 1,
description: this.order.description,
customer: this.order.customer,
deliveryDate: this.order.deliveryDate,
details: this.order.details,
total: this.order.total,
deposit: this.order.deposit,
status: this.order.status
};
try {
await axios.post(`/order/add`, data);
this.dialog = false;
this.dialog2 = false;
this.getOrders();
this.clearOrder();
this.$store.dispatch("showAlert", {
status: true,
type: "success",
text: "Pedido creado"
});
} catch (err) {
errorAlertHandler(err, "No encontrado");
}
},
解决方案
推荐阅读
- google-apps-script - DocumentApp.openById(fileId) 失败
- c++ - 如何让用户在 C++ 中选择一个数字
- parameters - 查询字符串参数中 Swagger 中的错误解析器错误?
- java - 为什么这个查询结果会出现时间
- ios - 是否可以在 iOS 13 中向 UISearchBar 添加语音听写(麦克风图标)按钮?
- c++ - 为什么在按值返回时创建临时对象而不是在将值传递给函数参数时创建临时对象
- reactjs - 如何在 package.json 文件中构建我的 service worker?
- python - 在元素之间的可变数量的空格字符上将列表项拆分为多个项目
- android - 实施广告显示此错误:必须启用对待处理购买的支持
- javascript - Use Mixin in template with Nuxt