Having trouble retrieving data passed between functions

One of my Vue components looks like this:

    import '../forms/form.js'
    import '../forms/errors.js'

    export default{
        data(){
            return{
                form: new NewForm({
                    email: '',
                    password: '',
                    intendedUrl: '',
                    message: ''
                })
            }
        },

        methods: {
            /**
             * Login the user
             */
            login(e) {
                e.preventDefault();

                this.form.startProcessing();

                this.$http.post('/login/authenticate', this.form)
                        .then(function(response) {
                            this.form.finishProcessing();
                        },
                        function(response) {
                            this.form.setErrors(response.data);
                        });
            }
        }
    }

The form.js implementation is as follows:

window.NewForm = function (data) {
    var form = this;

    $.extend(this, data);
    this.errors = new NewFormErrors();

    this.busy = false;
    this.successful = false;

    this.startProcessing = function () {
        form.errors.forget();
        form.busy = true;
        form.successful = false;
    };

    this.setErrors = function (errors) {
        console.log('okkk');
        form.busy = false;
        form.errors.set(errors);
    }
};

And the error.js file contains the following code:

window.NewFormErrors = function () {
    this.errors = {};

    this.set = function (errors) {
        console.log(errors);
        this.errors= errors;
    };
};

Although the this.form.startProcessing(); function seems to be working, I am facing difficulties in retrieving the data passed to the this.setErrors function. When I try to console.log(errors), it returns nothing or does not seem to execute properly.

Answer №1

Although I haven't fully replicated your solution, I believe the significance of the value in the deferred execution is worth exploring. Here's a modified version of the code that attempts to address this:

login(event) {
    event.preventDefault();
    const self = this ;
    this.form.startProcessing();

    this.$http.post('/login/authenticate', this.form)
         .then(function(response) {
                  self.form.finishProcessing();},
               function(response) {
                  self.form.setErrors(response.data); });
   }

I trust this adjustment will be beneficial.

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 implement a dynamic sidebar component using React and Material UI?

I have been attempting to implement a responsive sidebar using React Material Design, but I am struggling to achieve the desired outcome. The main goal is for the page content to remain responsive when the sidebar is opened, without overlapping on the pag ...

Adjust the width of a textarea dynamically as you type by detecting keyup events

Two text areas have the same text formatting. Their IDs are textareaA and textareaB. I've successfully added functionality to resize textareaA on keyup. How can I make textareaB resize its WIDTH while the user is typing in textareaA? Here's wha ...

`Where to include controller.js scripts in an Angular application`

As I dive into learning angular.js with .NET MVC, one major issue that keeps coming up is the fact that many tutorials advise referencing multiple controllers and services in the main (_Layout) page, which can make it quite messy. Although it may seem sim ...

Javascript promises executing in a mixed-up sequence

Utilizing Javascript's native Promise, I created a modified version of fs.readFile called readFileAsync. This function reads and parses a JSON file, returning the object when resolving. function readFileAsync(file, options) { return new Promise(fun ...

Retrieve the current date in the format of dd/mm/yyyy using AJAX request

var currentDate = new Date(); var todayDate = currentDate.getDate() + '/' + monthNames[currentDate.getMonth()] + '/' + currentDate.getFullYear(); This is my current date retrieval method. It works well but has a minor issue. For to ...

What could be causing the lack of functionality for my button click in my JavaScript and HTML setup?

Currently, I am attempting to implement a functionality where I have two buttons at the top of my page. One button displays "French" by default, and when I click on the "English" button, it should replace the text with "French" using show and hide methods. ...

propagate the previous state using a variable

Currently, I am in the process of refactoring a codebase but have hit a roadblock. My main aim is to update the state when the onChange event of a select box occurs. Specifically, the parameter searchCriteria in my handleFilterChange function is set to in ...

What is the best way to transfer a value from a function to a variable in VueJs?

Currently, I am receiving the base64 of an image URL. When passing the getImage function to the savepdf function and attempting to store the callback function's base64_data in a variable, an error is thrown: An error occurs - Cannot set property &a ...

Unusual activity observed in HTML5 contenteditable functionality

Within a list item, I have a span element. <ul> <li>text part 1 <span class="note">this is a note</span> text part 2 </li> <li>text part 3</li> </ul> When you double click on th ...

Transforming a two-column text document into a set of key-value pairs

Recently, I've been diving into the world of Node.js and Express. My current challenge involves converting a text file into key-value pairs. The approach I'm taking with my staircase program is as follows: https://i.sstatic.net/GPs200IQ.png Th ...

Utilizing the HTML button element within an ASP file combined with JavaScript can lead to the reloading of the

I am attempting to create a JavaScript function for an HTML button element. However, I have noticed that it causes the page to reload. Strangely, when I use an input with a button type, everything works perfectly. What could be causing this issue and why a ...

Activating the Speech Recognition feature in a form triggers the submission of a post

Currently, I am integrating webkitspeechRecongition into a form to allow users to enter data using their voice by pressing a button. However, a challenge arises when the call is triggered by a button within the form as it results in a post/submit action th ...

Tooltips for Images in Vega Charts

As a developer skilled in d3, I am navigating the world of VEGA charts and seeking guidance on how to incorporate an image in a tooltip. Do you have any suggestions for me? For example, considering this particular scenario: If we assume there is an addit ...

A guide on sending arrays from javascript to a Laravel controller through axios

Currently, I am utilizing Laravel 5.4 and Vue 2 to manage my data. I am facing an issue where I need to call a controller method using axios while passing two arrays. These arrays are crucial for making database requests in the controller and retrieving ne ...

Unable to import a Module in React without curly braces, even when using 'export default'

I recently stumbled upon a question regarding module imports in React. Some sources mentioned that using curly braces around imports can increase the bundle size by importing the entire library. However, I had been successfully importing modules without cu ...

JS - Reducing in size increases request size

I'm facing an issue with compressing my request - instead of reducing the size, it seems to be increasing it: const requestData = LZString.compress(JSON.stringify({ data: bigBase64StringHere })); await axios.post("api-endpoint", requestData, ...

A guide on modifying the color of various child components simultaneously by a single child component in Vue

How can you update multiple (in this case just 2) child components belonging to different parent components when an action is triggered by one of the children? For instance, there is a component, named <component-one/>. Inside this component, there ...

Stylized search input inspired by Pinterest with a bubbly design

When it comes to my search bar, I want the user's entered keywords to be displayed within a bubble that has a delete option when they press space to add another keyword. This functionality is similar to what Pinterest does with their search bar, as de ...

``There is an issue with the Nodejs required module variable not updating even after deleting

Feeling puzzled about the situation. Module 2 is supposed to require a variable from module 1, but even after deleting the cache, the variable in module 2 refuses to update when changes are made. Sample Module 1 var num = 6 function changeNum(){ num = ...

Prevent Vue.js from allowing new lines to be added in a contenteditable element

Is there a way to stop the Enter key from creating a new line in a contenteditable div? I attempted using @keyup.enter.prevent, but it did not work. I also tried @keyup.enter.prevent & @keyup.enter.stop.prevent with no success. ...