Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Stimulus.js private function returns undefined

I call a private function _checkCompletion() in a Stimulus.js controller. The private function sends an Ajax call and given its result, it should return true or false. However, the allValidated variable is undefined after the private function is called.

Here is my stimulus method:

nextQuestion() {
    var container = this.containerTarget;

    var moduleSlug = this.continueBtnTarget.getAttribute("data-course-module-slug");
    var allValidated = this._checkCompletion(moduleSlug);
    if(allValidated == true) {  
        container.classList.remove("active");

        document.getElementById("quiz-result-container").style.display = "block";
    }
}

Here is the private function:

_checkCompletion(moduleSlug) {
    $.ajax({
        url: "/check_module_completion",
        method: 'post',
        beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
        data: {
            course_module_slug: moduleSlug
        },
        success: function(data) {
            console.log(data['value']);
            if (data['value'] == 'true') {
                return true;
            } else {
                return false;
            }
        },
        error: function() {
            console.log("Ajax error!");
        },
    })

}

Any idea what I am doing wrong? I tried to change the private function into a normal function after the stimulus controller function _checkCompletion() { ... } but the allValidated variable is also undefined.

I verified and moduleSlug is defined.

Thanks.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!