首页 > 解决方案 > 为什么我的数组在发布请求后返回未定义?

问题描述

我有一个函数可以将详细信息添加到详细信息数组中,第二个函数可以向服务器发出 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");
    }
},

标签: javascriptarraysvue.jsvuetify.js

解决方案


推荐阅读