【问题标题】:How can i make json file like this when i input输入时如何制作这样的json文件
【发布时间】:2020-11-03 02:02:22
【问题描述】:

我怎样才能使 cart_items 像我的 expetation.. 只是它不再.. 我的问题只是它 :D 我只是想让我的 cart_items 像这样.. 希望你能帮助我,谢谢。我做错了方法吗?还有一件事,我想在 cart_items

中制作 qty

这是我的期望

"cart": [
{
  "id": 1,
  "date": "12/10/2020",
  "store": {
    "id": 1,
    "name": "Dirumah Aja",
    "promo": 1
  },
  "cart_items": [
    {
      "id": 1,
      "product": {
        "id": 1,
        "name": "Bakso Urat",
        "price": 10000,
        "promo": {
          "nama": "promo"
        }
      },
      "qty": 5
    }
  ]
}
]

这就是我得到的

"cart": [
{
      "cart_items": {
        "name": "Steak Sapi Impor",
        "price": "38000",
        "stock": "4",
        "image": "https://firebasestorage.googleapis.com/v0/b/francise-fb70a.appspot.com/o/steak.jpg?alt=media&token=46e0d769-96d3-440f-8edb-5fce2481ace0",
        "promo": 3,
        "id": 8,
        "qty": 1
      },
      "store": {
        "name": "Amanda Foods Store",
        "email": "amanda@food.com",
        "store_image": "https://firebasestorage.googleapis.com/v0/b/francise-fb70a.appspot.com/o/full_hd_retina.jpeg?alt=media&token=3e602e86-661b-48ee-9e9c-af9f94a170d1",
        "product": [
          5,
          7,
          8,
          2
        ],
        "store_promo": 1,
        "location": {
          "street_name": "Jl. Kebon Gedang II B",
          "province": "Jawa Barat",
          "city": "Bandung",
          "post_code": "40285"
        },
        "id": 1
      },
      "date_order": "Nov 03 2020 08:48:03",
      "id": 2
    }
  ]

这是我的数据

data() {
        return {
            promo_id: [],
            promo_partner: [],
            products: {},
            qty: 1,
            cart_items: [
                {}
            ]
        };

这是我的方法

addToCart() {
            const date = (new Date()).toString().split(' ').splice(1,4).join(' ')

            this.products.cart_items = this.product;
            this.products.cart_items.qty = this.qty;
            this.products.store = this.partner;
            this.products.date_order = date;

            console.log(this.cart_items)

            axios
                .post("http://localhost:3000/cart/", this.products)
                .then(() => {
                    swal("Belanja Berhasil!", {
                        icon: "success",
                    });
                })
                .catch((error) => console.log(error));
        }


    }

【问题讨论】:

    标签: javascript json vue.js


    【解决方案1】:

    您需要使用.push() 将项目添加到数组中。您正在将数组替换为 this.product

    if (!this.products.cart_items) { // initialize cart_items if necessary
        this.products.cart_items = [];
    }
    this.products.cart_items.push({id: this.product.id, product: this.product, qty: this.qty});
    

    【讨论】:

    • 我收到类似这样的错误 [Vue 警告]: v-on 处理程序中的错误:“TypeError:无法读取未定义的属性 'push'”
    • 你说你的数据包含cart_items: [{}]这不是初始化吗?我不知道 vue.js。
    • 您需要提前将其初始化为一个空数组。
    • 我更新了答案以初始化属性(如果尚未设置)。
    • 你好 @Barmar 我不知道该说什么,但是 ITSSSSSSSSSSSSS WORRRRRRRRRRRRRRRRRRRRRRRRRRKSSSSSSSSSSSSS 谢谢你 MUCCCCCCCCCCCH :D 上帝保佑你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 2011-06-17
    • 2017-12-05
    • 2013-04-12
    相关资源
    最近更新 更多