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

129
Vistas
Pass value from response into mutationObserver()

I'm making a POST to check if a user's login details are valid and if not, I want to display the error message, that is caught, (variable errorMessage).

I'm using mutationObserver() to wait for a node to appear in the DOM, where I will then insert my error message. (Unfortunately, I have to write my code this way, due to the application we are using at work).

My question is, how do I pass errorMessage from my catch() method, into the mutationObserver()?

I tried passing this into observer.observer(), but I don't get the errorMessage console.logged. Just the following:

It's in the DOM! MutationObserver {}

My code is below. How do I do this?

Alternatively, I was thinking of storing the errorMessage as a global variable instead. And then accessing that in mutationObserver().

But I just wondered, if I can actually pass another variable into mutationObserver()?

Thanks,

const submitButton = document.querySelector(".login__btn");

// How do I pass errorMessage into MutationObserver()
const observer = new MutationObserver(function(mutations, errorMessage) {
    if (document.contains(document.querySelector(".error-container"))) {
         console.log("It's in the DOM!", errorMessage);
     }
 });

submitButton.addEventListener("click", function() {
    console.log("clicked...");

    let data = {
        email: "my@emailaddress.com",
        password: document.getElementById("password").value,
        rememberMe: false,
        captcha: ""
   };

    fetch("https://www.PRIVATEAPI.com/holidays/_api/v1.0/account/login", {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        mode: 'cors',
        credentials: "include",
        body: JSON.stringify(data)
    })
    .then(response => {
        if (!response.ok) {
            return Promise.reject(response);
        }
        return response.json();
    })
    .then(data => {
        console.log("Success");
        console.log(data);
    })
    .catch(error => {
        if (typeof error.json === "function") {
            error.json().then(jsonError => {
                console.log("Error from API...");
                console.log(jsonError.innerErrors[0].message);

                // How do I pass this variable into the mutationObserver() function?
                let errorMessage = jsonError.innerErrors[0].message;

                observer.observe(document, {attributes: false, childList: true, characterData: false, subtree:true}, errorMessage);
            })
        };
    });
});
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