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

147
Vistas
I am trying to make an AJAX fetch request and I keep getting an error "Uncaught SyntaxError: Unexpected token ')'"

I can't figure out where to put the curly brackets or parenthesis to make it work correctly. At first I thought the server was down or something but then I managed to console log the data. I kept uninstalling and reinstalling this visual studio code extension called "Bracket Colorizer" to try and solve the but I am all out of juice.

document.addEventListener('DOMContentLoaded', () => {
  const title = document.createElement('h1');
  title.innerText = 'Online Chatroom';
  document.querySelector('body').appendChild(title);
  // make AJAX call here.... 
  fetch('https://curriculum-api.codesmith.io/messages')
    .then(data => data.json())
    .then(data => {
      const main = document.querySelector('main')
      for (let i = 0; i < data.length; i++) {   
        writeHTML(data[i], main)
      // console.log(data);
      });
    };
};

Here is the rest of the code me and my tutor from wyzant worked on together if it helps.

document.querySelector('form').addEventListener('submit', sendMessage) 

function writeHTML(message, htmlNode) {
  let messageContainer = document.createElement('div')

  let messageText = document.createElement('p')
  messageText.innerHTML = message.message
  messageContainer.appendChild(messageText)

  let messageTime = document.createElement('span')
  messageTime.classList.add('time')
  messageTime.innerText = message.created_at
  messageContainer.appendChild(messageTime)

  linebreak = document.createElement("br");
  messageContainer.appendChild(linebreak);
  // // document.innerHTML(<br>)

  let createdBy = document.createElement('span')
  createdBy.classList.add('message_sender')
  createdBy.innerText = message.created_by
  messageContainer.appendChild(createdBy)

  htmlNode.appendChild(messageContainer)

}

function sendMessage(event) {
  event.preventDefault()
  let newMessage = document.querySelector('textarea').value
  let data = {
    message: newMessage,
    //figure out how to add another text box and insert that data here
    created_by: "Matthew",
    created_at: Date.now()
  }
  fetch('https://curriculum-api.codesmith.io/messages', {
    method: 'POST',
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  }).then(function(response){
    return response.json()
  })
  .then(function(response){
    const main = document.querySelector('main')
    writeHTML(response[0], main)
  })
  .catch(function(error){
    console.error(error)
  })
};

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This parenthesis is misplaced:

  // console.log(data);
  }); <-- this one
};


  // console.log(data);
  }
}); <-- should be here
about 4 years ago · Juan Pablo Isaza Denunciar

0

You had two parenthesis misplaced, you can check with the code below:

document.addEventListener('DOMContentLoaded', () => {
  const title = document.createElement('h1');
  title.innerText = 'Online Chatroom';
  document.querySelector('body').appendChild(title);
  // make AJAX call here.... 
  fetch('https://curriculum-api.codesmith.io/messages')
    .then(data => data.json())
    .then(data => {
      const main = document.querySelector('main')
      for (let i = 0; i < data.length; i++) {   
        writeHTML(data[i], main)
      // console.log(data);
      }
    })
    })

about 4 years ago · Juan Pablo Isaza Denunciar
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