Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda