0

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 }));
};
  • Seems like you haven't included either title or description in your postman request. The error is because after `res.status(400).json({ message: "content can not empty" })` the code is still being executed and causing to re-send the response later in the code. You should return from the function after `that res.status(400).json(...)`. – Han Moe Htet May 10 '22 at 09:38
  • Exactly, this error occurs when you're sending back two responses. If req.body is blank, the question is a) did you activate a body parser? b) are you sending POST parameters? –  May 10 '22 at 09:39
  • @HanMoeHtet I tried sending the data directly from the "raw" tab of postaman and it seems to work. – ZeyennaStudy May 10 '22 at 09:56
  • If you have a new question: Ask a new question. Don't edit an existing question to be a completely different question that makes the comment history and any answers no longer make sense. – Quentin May 10 '22 at 09:57
  • @ChrisG I understood my mistake, the answer expects a json format and not text so you have to use the raw part of postman. My request is well created. – ZeyennaStudy May 10 '22 at 10:01

1 Answers1

1

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 }));
};