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

125
Views
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
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!