Passing an empty object in axios with Vue.js

When sending an object from this.productDetails in an axios.post() request, I noticed that the object appears empty when inspected in the browser's network tab.

Here's the Axios call:

        async addProduct(){
            console.log('product_details', this.productDetails)
            const apiResponse = await axios.post('add_product',{
                productDetails : this.productDetails
            })

            const response = apiResponse.data
        },

Despite logging the object using

console.log('product_details', this.productDetails)
, which shows that it contains the actual object: https://i.sstatic.net/SlhLP.png

This is my Laravel route:

    Route::post('add_product',[PackageSubscriptionController::class, 'addProduct']);

Here's the Network: https://i.sstatic.net/eCCQD.png

Answer №1

The issue arose from the state of the object. Initially, I set this.productDetails as an empty array within a parent component and then passed it to a child component where the data would be loaded into this.productDetails. However, during the axios.post call, the initial state of the object (which was empty) was being passed. I am still trying to determine how to pass the correct state of the object and would greatly appreciate any guidance on this topic. Any references to further understand states would be welcomed.

Here is the modification I made in the axios.post method that resolved the issue for me. I understand it may be a temporary solution, but due to upcoming deadlines, I chose to proceed with it for now.

        async addProduct(){
            const data          = this.productDetails
            const apiResponse   = await axios.post('add_product',{
                                        name            : data.name,
                                        description     : data.description,
                                        price           : data.price,
                                        livemode        : data.livemode,
                                        active          : data.active,
                                    })
            const response      = apiResponse.data
        },

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

What is the best way to handle code versioning using Django, Angular2, and Webpack?

Currently, I am utilizing Django in conjunction with Angular 2 and Webpack. Within Django, I have set up a URL to display my application at http://example.com/user/beta. Initially, my index.html file is rendered, which contains my Angular 2 components. Wit ...

What mistakes am I making in including/injecting functions in AngularJS controllers and factories?

I'm encountering an issue in Angular where I am struggling to inject my factory into my controller. The error message I'm receiving is "Cannot read property 'validar' of undefined". In my project, I have two files - ServiceUtil.js which ...

Differences in SVG rendering between Chrome and Firefox

I am eager to create a diagram on my website using SVG. To ensure the diagram is displayed in full screen, I am utilizing availHeight and availWidth to determine the client screen's height and width, then adjust the scaling accordingly. My current sc ...

Using Selenium Webdriver to target and trigger an onclick event through a CSS selector on a flight booking

I've been running an automation test on the website . When searching for a flight, I encountered an issue where I was unable to click on a particular flight. I initially tried using Xpath but it wasn't able to locate the element when it was at th ...

Determining age in a Class upon instance creation

Is there a way to encapsulate the age calculation function within the Member Class without resorting to global space? I want to automatically calculate and set an age when creating an instance. Currently, I have a function that works, but I'm curious ...

Generating a .png image using the data received from the client [node]

I need to create a highchart client-side and save a PNG of that chart server-side. After successfully generating the highchart and converting it to a PNG using the following function: function saveThumbnail(graph_name, chart) { canvg(document.getEleme ...

Is it possible to use function declaration and function expression interchangeably?

As I dive into learning about functions in Javascript, one thing that's causing confusion for me is the difference between function declaration and function expression. For example, if we take a look at this code snippet: function callFunction(fn) { ...

Jasmine attempting to access a nonexistent property

I created a very basic component: import { Component } from '@angular/core'; @Component({ selector: 'loading', templateUrl: './loading.component.html', styleUrls: ['./loading.component.scss'] }) export ...

User-Preferred Dark Mode Toggle Using Material-UI and Fallback Option

I am working on implementing a toggle for "dark mode" that is functioning well. However, I want the initial state to reflect the user's dark/light preference. The problem is that prefersDarkMode seems to be set to false when the page loads. It only c ...

Search through array elements that are nested deeply

Consider the following scenario: an array is provided as input containing various objects with nested elements. The goal is to filter this array in JavaScript and obtain a new array consisting only of objects where the key "navigation" has a value of true. ...

Fetch request encountered a 500 error due to the absence of the 'Access-Control-Allow-Origin' header on the requested resource

Currently, I am in the process of developing a front-end web application using create-react-app and need to make a request to the ProPublica API. The fetch call code snippet is as follows: export const fetchSenators = () => dispatch => { fetch(' ...

Having difficulty removing Vue from my Ubuntu system

Hey there, I've been using WSL and trying to run my Vue project with version 2.9.0, but when I check the version with vue -V, it shows 2.9.6. I've attempted uninstalling Vue CLI using both npm uninstall @vue/cli and npm uninstall -g @vue/cli, bu ...

Generate a randomly structured 2D array called "Array" using JavaScript

Can anyone help me with extracting a random array from a 2D named array? I've tried several solutions but none of them seem to work. var sites = []; sites['apple'] = [ 'green' , 'red' , 'blue' ]; sites['o ...

Guide on making a submit and close button within an overlay

Seeking a button to both submit and close an overlay window. The button code is as follows: <a href="javascript:additem('Info to add here', 10.00,1);" role="button"><button class="purchase btn btn-warning">Select</button></ ...

Experiencing difficulties with arrays in JavaScript while using React Native

Programming Challenge let allURL = [] const [toReadURL, setToReadURL] = useState([]) useEffect(() => { const listReference = sReference(storage, parameterKey) // Retrieve all the prefixes and items. listAll(listReference) .then((res ...

What are the advantages of using history.push or another method from react-router-dom compared to simply assigning the path to window.location.pathname?

When I need to navigate within my code, I find it more convenient to simply assign the desired path to window.location.pathname. Can this approach have any drawbacks? ...

Retrieve Browser Screen Width and Height using MVC3 Razor in the View

I am facing a challenge on my website where I need to generate a Bitmap dynamically and ensure it is rendered according to the width and height of the browser so that there are no overflow issues on the page. Although I have successfully created an image ...

Node.js setInterval is a method used to repeatedly execute a function

I have a code snippet for an http request that is supposed to run every one minute. However, I am encountering an issue with the following error: "Error: listen EADDRINUSE". Here is my current code: var express = require("express"); var app = express(); v ...

Incorporate JSON data using jQuery's AJAX in MVC3

I need assistance with parsing the JSON data retrieved from a webservice through my controller. Currently, I am displaying the entire JSON string in a div as text. However, I only want to extract specific values such as "color" and "size". I am unsure of ...

A guide to accessing a property value in Angular 6 from an object using a string key

In my Angular application, I am receiving an object with multiple arrays from a REST service. I need to access the value from the incoming object based on the property name. Here is what the object looks like: data{ array1:{}, array2:{}, array3:{} } Th ...