Troubleshooting Vue.js data assignment issues

I'm attempting to implement basic form validation using Laravel 5.3 and Vue.js.

Laravel controller:

 public function test(\Illuminate\Http\Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'date' => 'required'
        ]);
...
    }

Vue data:

let app = new Vue({
        el: '#app',
        data: {
            submitted: false,
            errors: [],
            post: {
                name: '',
                date: '',
            },
        },

Vue post:

Vue.http.post('url', post).then((response) => {
    // form submission successful, reset post data and set submitted to true
    app.post = {
        name: '',
        date: '',
    };

    // clear previous form errors
    app.$set('errors', '');

    app.submitted = true;

}, (response) => {
    // form submission failed, pass form errors to errors array
    console.log(response.data.name); //"The Name field is required."
    app.$set('errors', response.data); // TypeError
});

I am encountering

TypeError: can't assign to properties of (new String("errors")): not an object

when using

app.$set('errors', response.data);

How can I correct this issue?

Answer №1

Make sure to reference vm.$set(object, key, value). It seems like you're attempting to append a property to 'errors' by using response.data as the key.

The correct format would be

app.$set(app, 'errors', response.data)
. Alternatively, you could use app.errors = response.data and achieve the same outcome.

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

Showing a variety of pictures within a specified time frame

In my CSS, I have defined classes that allow me to display different background images on a page at set intervals: .image-fader { width: 300px; height: 300px; } .image-fader img { position: absolute; top: 0px; left: 0px; animation-name: imagefade; ...

Implementing conditional put on DynamoDB - step by step guide

In my restaurant / bar, I have a DynamoDB table named Cheque to keep track of the cheques for each table. I need to apply certain conditions when creating a new entry in this table: tableNumber should not already exist restaurantId should not already exi ...

Tips for avoiding template errors when retrieving data using Nuxt's fetch() method

After transitioning my vue app to a nuxt app, I encountered an issue with vue-slick-corousel. In the original vue app, everything worked smoothly with vue-slick-carousel, but after making the necessary modifications for nuxt, the carousel wouldn't dis ...

Unveiling Vue3: A Guide to Retrieving the Entire Component State, Along with its Properties, in the Vue Error Handler - Tips on Transforming a Vue Component Instance into

Incorporating Vue3 with the composition API and Bugsnag for error management has been a game-changer. When an error arises, my goal is to send the entire component state where the error occurred to Bugsnag (specifically, the object returned from setup()). ...

Generating variable names dynamically in JavaScript

To prevent a javascript heap issue, I implement the usage of multiple arrays including 'family1', 'family2','family3' and 'dogs1', 'dogs2', 'dogs3'. For instance, you can use 'family1 and dog ...

Divide a nested list into individual lists

I am working on a navigation menu with nested lists and I need to split the nested lists using jQuery while keeping the original headings intact. Can anyone help me achieve this? Below is the HTML code: <ul id="bigList"> <li><a href="#"& ...

Mysterious Angular Provider: $http

Is there a way to include the $http service in the config method? When I try to do so, I encounter an "unknown Provider" error message. This is the snippet of code causing the issue: .config(function($http, $routeProvider, $provide) { $http.get("samp ...

What is the best way to show an image on the screen once a submit button is clicked?

I have a hidden loader-bar gif that I want to display when the user submits a form. Here is the code: <p class="loadingImg" style="display:none;"><img src="/design/styles/_shared/classic-loader.gif" alt="" /></p> Is there a way to ...

Utilize the precise Kendo chart library files rather than relying on the kendo.all.min.js file

My application currently uses the Kendo chart, and for this purpose, it utilizes the "kendo.all.min.js" file which is quite large at 2.5 MB. To optimize the speed performance of the application, I decided to only include specific Kendo chart libraries. In ...

3D Object Anchored in Environment - Three.js

In my current scene, I have two objects at play. Utilizing the LeapTrackballControls library for camera movement creates a dynamic where one object appears to rotate based on hand movements. However, an issue arises as the second object also moves along w ...

Is there a way for me to automatically choose the second checkbox after selecting the first one?

<input type="checkbox" id="first" class="vh-item" value="first" v-model="checkedNames" /> <label class="list-specific" for="first">1stcheck</label> <input type="checkbox" id="second" class="vh-item" v-model="checkedNames" value="secon ...

The ElementNotVisibleException error was triggered because the element is currently hidden from view, making it unable to be interacted with through the command

I'm facing an issue when trying to clear the text box. The error message I am receiving is: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.14 se ...

Having trouble reaching the upload file in PHP

I am attempting to transfer files to a remote server using JavaScript, with PHP as the backend. The JavaScript code seems to be functioning properly, but on the PHP side, the $_FILES and $_POST arrays are empty. However, the $_SERVER array contains some da ...

Troubleshooting: Angular 2 View not reflecting changes after array push

I have encountered an issue with my two child components. They are both meant to share data from a json file that I load using the http.get/subscribe method. Oddly enough, when I try to push new data into the array, it doesn't seem to update in the vi ...

Securing Azure B2C user access with Auth Flow using PKCE, MSAL v2, Vue.js, & Azure Functions

Our application utilizes Vue.js, Azure B2C Tenant, and Azure Functions (C#) to authenticate users. We implement the Auth Flow with PKCE and utilize the MSAL v2 library on the front-end, specifically the npm package @azure/msal-browser for MSAL. The challe ...

Displaying JavaScript Array Elements in an Aligned Table Format

Unfortunately, I accidentally deleted my previous post and now need to repost it. I have a code that is functioning well, thanks to the great help I received here. However, the output of the array players is not neatly aligned with each other in a table f ...

Is there a way to extract rows from a React MUI DataGrid that are identical to how they are displayed, including any filtering and sorting applied?

My goal is to make a selected row move up and down on arrow clicks, and in order to achieve this, I need to retrieve rows from the MUI DataGrid. I am using the useGridApiRef hook to do so, ensuring that the rows are filtered and sorted accordingly to match ...

Creating a multi-page app using React: A comprehensive guide

I am in the process of developing an application using NodeJS, and I have decided to incorporate React for creating interactive components within the app. However, my goal is to avoid turning it into a single-page application. How can I effectively distri ...

Can you define the "tab location" in an HTML document using React?

Consider this component I have: https://i.stack.imgur.com/rAeHZ.png React.createClass({ getInitialState: function() { return {pg: 0}; }, nextPage: function(){ this.setState({ pg: this.state.pg+1} ) }, rend ...

Attempting to confirm the validity of my form

I have been attempting to validate my form in JSP, but unfortunately it is not working as expected. The form runs fine, however, the function 'validatemark' from my JavaScript file is not being called. Below is a snippet of my code: <%@ ...