M
M
MatrikLog2021-09-27 13:45:58
JavaScript
MatrikLog, 2021-09-27 13:45:58

Why does an empty object come from the front to the back?

The last question was deleted by the moderator (probably for the screenshots of the code)
Why does an empty object arrive from the front to the back?
Hello everyone, when sending a request from the front to the back, an empty object arrives in req.body on the back. I would like to find out why. Here is the code of what I send from the front:

const postNewTask = async (data) => {
    const res = await fetch(API_URL + "/reminds", {
      method: "POST",
      body: JSON.stringify(data)

    });
    return res;
  };


Here is the data in the request:
{name: "123", text: "123", taskId: "Программирование"}

Here is the data model that I expect on the back (I use sequelize , postgresql db
const RemindsTasks = sequelize.define("reminds", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  name: { type: DataTypes.STRING, unique: true, allowNull: false },
  text: { type: DataTypes.STRING, unique: true, allowNull: false },
  typeId: { type: DataTypes.STRING, unique: true, allowNull: true },
});


Here is the controller code:
class RemindsController {
  async create(req, res) {
    const { name , text , typeId } = req.body;
    console.log('req body',req.body)
    console.log(req.body)
    const remindsItem = await RemindsTasks.create({ name , text ,typeId });
    return res.json(remindsItem);
  }

  async getAll(req, res) {
    const reminds = await RemindsTasks.findAll();
    return res.json(reminds);
  }
}

In the main module, everything is OK, app.use(express.json()); there is, if sent via postman everything works.
What could be the problem ? what am I doing wrong ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question