Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda