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

280
Views
JavaScript: Uncaught ReferenceError: username is not defined

i start to programm a chat but i became always this error:

Uncaught ReferenceError: username is not defined

But I defined const username = user.displayName;

this is a part of my code:

function authStateListener() {
  firebase.auth().onAuthStateChanged((user) => {
    if (user) {
      const username = user.displayName;
    } else {
      window.location.href = "../pages/login.html";
    }
  });
};

and there is the error:

fetchChat.on("child_added", function (snapshot) {
  const messages = snapshot.val();

  const now = (new Date().toLocaleDateString()).split(".");
  const t = (messages.date).split(".");

  if( t[0] < now[0]) {
      const message = `<li class=${username === messages.username ? "sent" : "receive"}><span class="name">${messages.username} </span><br>${messages.message}<br><span class="time">${t[0] - now[0]} Days ago</span></li>`;
      addMsg(message);
  } else {
      const message = `<li class=${username === messages.username ? "sent" : "receive"}><span class="name">${messages.username} </span><br>${messages.message}<br><span class="time">${messages.time}</span></li>`;
      addMsg(message);
  }
  //const message = `<li class=${username === messages.username ? "sent" : "receive"}></span>${messages.message}</li>`;
});

I hope you can help me

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!