Issue with Vue.js: Difficulty sending an array of values to an endpoint

I am currently in the process of learning Vue in order to complete my project, which has a Java Spring backend. The endpoint I am working with expects an object with the following values:

LocalDate date;
Long buyerId;
Long supplierId;
List<OrderDetailsDto> orderDetails;

In Vue, my object structure is as follows:

order: {
          date: '',
          buyerId: 0,
          supplierId: 0,
          orderDetails: [
              {
                  quantity: 0,
                  product: {
                      id: 0
                  }
              }
          ]
      }

The input fields for quantity and products that I have set up are causing an issue:

           <div class="form-group">
                <label>Ilość (m3)</label>
                <input type="number" id="quantity" class="form-control" v-model="order.orderDetails.quantity"/>
            </div>

            <div class="form-group">
                <label>Wybierz product</label>
                <select v-model="order.orderDetails.product">
                <option
                    v-bind:value="{id: product.id, name: product.product}"
                    v-for="product in products"
                    v-bind:key="product.id"
                >{{ product.product }}</option>
                </select>
            </div>

When I check the request object just before sending it (using console.log), it looks like this:

{__ob__: Observer}
  date: (...)
  buyerId: (...)
  supplierId: (...)
  orderDetails: Array(1)
    quantity: "22"
    product: Object
    0:
    quantity: 0
    product: Object

This is where the issue arises. The variables I have declared in Vue are at index [0] in orderDetails. The values such as quantity: "22", product: Object are not being sent, as the endpoint thinks the array is empty. If I remove the values from the array in the Vue object, the console.log appears correct but the endpoint still does not receive the values in the array.

Does anyone have any ideas on how to solve this problem?

Answer №1

include

data: {currquantity:0, currproduct: {id: null, name: null}}

           <div class="form-group">
                <label>Amount (m3)</label>
                <input type="number" id="quantity" class="form-control" v-model="currproduct.quantity"/>
            </div>

            <div class="form-group">
                <label>Select product</label>
                <select @change="order.orderDetails.push({quantity: currquantity, ...currproduct})" v-model="currproduct">
                <option
                    v-bind:value="{id: product.id, name: product.product}"
                    v-for="product in products"
                    v-bind:key="product.id"
                >{{ product.product }}</option>
                </select>
            </div>

To keep track of what has been added and allow deletion of items, you can create a structure similar to the one below:

<ul>
<li v-for="(detail, i) in order.orderDetails">{{JSON.stringify(detail)}}
  <button @click="order.orderDetails.splice(i,1)">X</button>
</li>
</ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Bringing together a dual $exists in a MongoDB search

I'm trying to locate documents that have certain fields using the $exists operator. Specifically, I am focusing on two fields: payload.fields.MDI_CC_DIAG_DTC_LIST and payload.asset. If I only want to find documents with the first field, I execute thi ...

Utilizing Laravel and Jquery UI for asynchronous communication with the server, passing data from a post request to

I recently constructed a basic Jquery UI slider: $("#sliderNumCh").slider({ range: "min", min: 0, max: 20, step: 1, value: 20, change : function(e, slider){ $('#sliderAppendNumCh'). ...

The Axios post request was met with a 400 error, indicating a bad request

When testing an API using Postman, I encountered an issue when trying to write a request with axios. The original request in Postman was successful and looked like this: In my attempt to recreate the request in javascript using axios, I wrote the followin ...

Can you offer advice on creating jasmine tests for an Angular controller and service similar to this?

I've been struggling with this issue for hours, mainly because I'm still new to Jasmine and AngularJS. The problem lies within my controller. angular.module('toadlane.controllers', []). controller('MainController', functi ...

Working with time durations in Moment.js to manipulate datetime

How can I properly combine the variable secondsToMinutes with the startdate? The value of secondsToMinutes is "3:20" and the startDate is "2:00 PM". The desired endDate should be "2:03:20 PM". Despite my efforts, I keep encountering errors every time I at ...

Changing the image source dynamically at runtime using JavaScript and jQuery

Is it possible to dynamically change the source of an image using jQuery during runtime? I have set up a jsfiddle to demonstrate my question. I am attempting to load the image specified in the variable $newsrc when a button is clicked. However, I am unsure ...

Mastering the onKeyPress event for Material UI text fields: A step-by-step guide

I have a TextField component from Material UI as shown below: <TextField id="todo-textarea" label="Enter new todo" placeholder="ToDo" onChange={this.props.toDoOnChange} onKeyPress={(ev) => { ...

Is there a way to modify the FixedTableHeader jQuery plugin to include a fixed first column in addition to the fixed header?

I've been experimenting with a jQuery plugin I found at to create a stylish table with fixed headers, sorting options, and other interesting features. While the plugin is great, I also considered using jqGrid for its impressive capabilities. However, ...

A guide on calculating the number of days between two dates using Angular 12

I am currently utilizing Angular 12 for my project. The calculation of days based on two dates seems to be working perfectly fine in Chrome, but unfortunately, it fails in Firefox. In my TypeScript file: getDaysCount(firstDate: any, secondDate: any) { ...

The absence of FormData.entries in submit is a limitation of the Vue framework

I recently created a Vue-App that consists of a simple form with just one <input name"surname"> and a <button type="submit">. The use case is to input "myname" and submit the form. However, when I initialize new FormData( ...

Using Express.js and Angular for user authentication and session management

In my current project, I am utilizing expressjs and angularjs to create an app. The setup involves expressjs serving a single .html file that houses an angular single-page-application. All routing is handled by angularjs, while expressjs provides web servi ...

Is it possible to iterate through div elements using .each while incorporating .append within an AJAX call?

After sending AJAX requests and receiving HTML with multiple div elements (.card), I am using .append to add new .card elements after each request, creating an infinite scroll effect. However, I am facing issues when trying to use .each to iterate over all ...

Creating a secure ZIP file with password protection using node.js

Currently, I am faced with the challenge of creating a ZIP file in node.js with password protection. Despite using the "node-zip" module for this task, it lacks the functionality to provide password protection: var zip = new require('node-zip') ...

Exploring the Dynamic Routing Features of Next.js

After diving into Next.js, I found the learning curve to be manageable since React was already familiar territory for me. Currently, my focus is on implementing dynamic routing in my application - Starting with my live/index.js file - (Where I utilize ge ...

Issues with styled-components media queries not functioning as expected

While working on my React project with styled components, I have encountered an issue where media queries are not being applied. Interestingly, the snippet below works perfectly when using regular CSS: import styled from 'styled-components'; exp ...

Steps for initializing a Vue component instance by passing parameters

Being a novice in the realm of Vue, I am eager to gain knowledge on how to effectively create and reuse Vue components. However, I am encountering an issue where the initial data passed to a component does not update upon a click event. Shown below is a ...

Managing multiple sets of radio buttons using the useState hook

Within my renderUpgrades-function, I handle the options of an item by including them in radio-button-groups. Each item has multiple options and each option has its own radio-button-group. Typically, a radio-button-group can be managed using useState, wit ...

What could be causing my JSON product list to not load properly?

My list is not loading and I can't figure out why. I've included my json, jquery, and HTML below. The console isn't showing any errors, but the list is still blank. Any help would be greatly appreciated as I am new to working with json. Than ...

The mousedown event handler in Three.js disrupts the focus on the input field

Here is a line of code in threejs: document.addEventListener('mousedown', onDocumentMouseDown, false); This particular code snippet causes the input field to lose focus when clicked. This can be problematic, for instance, when there is a canva ...

Extract keys from the string

Is there a better way to extract keys from a string? const {Builder, By, Key, until} = require('selenium-webdriver'); ... const obj = {val: 'Key.SPACE'} if(obj.val.startsWith('Key.'))obj.val = eval(obj.val); (...).sendKeys(obj ...