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

166
Views
Errorcode: 'ERR_HTTP_HEADERS_SENT' when I send my request POST

I don't understand why request to create an object is no longer valid. I create models with mysql and sequelize.

I fill in the user's token for each request, to connect, retrieve his profile, retrieve all the articles from the database and it works, but now I can't create any more articles.

I did console.log(req.body)of my function but I have this : {}. My function gives me a 400 error from my first condition.When I send my request, on VSC I have " code: 'ERR_HTTP_HEADERS_SENT'".

I checked the number of characters that should match the sequelize patterns, it's good. I filled in the 3 fields on postman, title, description and userId.

//*******Creating an article*******//
exports.createArticle = (req, res, next) => {
  //Nous allons renvoyer 2 paramêtre //
  const title = req.body.title;
  const description = req.body.description;

  console.log(req.body);
  // Fields must not be empty before sending //
  if (title == null || description == null) {
    res.status(400).json({ message: "content can not empty" });
  }
  console.log(req.body);
  //***Build the request body****/
  const article = Article.build({
    title: req.body.title,
    description: req.body.description,
    userId: req.userId,
  });
  console.log(article);

  //***Save new article***//
  article
    .save()
    .then(() => res.status(201).json({ article }))
    .catch((error) => res.status(400).json({ error }));
};

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As even after returning { message: "content can not empty" }, your code is continuing execution and sends another response, if you didn't provide title or description. Please modify your code as per below code

exports.createArticle = (req, res, next) => {
  //Nous allons renvoyer 2 paramêtre //
  const title = req.body.title;
  const description = req.body.description;

  console.log(req.body);
  // Fields must not be empty before sending //
  if (title == null || description == null) {
    res.status(400).json({ message: "content can not empty" });
    return;
  }
  console.log(req.body);
  //***Build the request body****/
  const article = Article.build({
    title: req.body.title,
    description: req.body.description,
    userId: req.userId,
  });
  console.log(article);

  //***Save new article***//
  article
    .save()
    .then(() => res.status(201).json({ article }))
    .catch((error) => res.status(400).json({ error }));
};

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!