我有一个将详细信息添加到详细信息数组中的函数,以及另一个向服务器发出发布请求的函数。我第一次运行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");
}
},