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

175
Views
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 answers
Answer question

0

This parenthesis is misplaced:

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


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

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 Report
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!