title: ReactJS + MySQL cannot create new object using api
body: <p>I am beginner in web dev and trying to create a Full stack project using Mysql Express React Node.js. I tried creating a new device using parameter like serialno(Primary key for db),brand and Modelname. When I ran the api in Postman it didn't read the input and gave message from Contoller js( message: &quot;Content can not be empty!&quot;)</p>
  <p><code>server.js</code></p>
  <pre><code>  const express = require(&quot;express&quot;);
    const bodyParser = require(&quot;body-parser&quot;);
    const cors = require(&quot;cors&quot;);
  
    const app = express();
  
    var corsOptions = {
      origin: &quot;http://localhost:8081&quot;
    };
  
    app.use(cors(corsOptions));
  
    // parse requests of content-type - application/json
    app.use(bodyParser.json());
  
    app.use(bodyParser.urlencoded({ extended: true }));
  
    const db = require(&quot;./app/models&quot;);
    db.sequelize.sync();
  
    // simple route
    app.get(&quot;/&quot;, (req, res) =&gt; {
      res.json({ message: &quot;Welcome &quot; });
    });
  
    require(&quot;./app/routes/routes&quot;)(app);  
  
    // set port, listen for requests
    const PORT = process.env.PORT || 3000;
    app.listen(PORT, () =&gt; {
      console.log(`Server is running on port ${PORT}.`);
    });
  </code></pre>
  <p><code>controller.js</code>(using the first create function, error message:&quot;Content can not be empty!&quot;)</p>
  <pre><code>const db = require(&quot;../models&quot;);
  const Device = db.devices;
  const Op = db.Sequelize.Op;
  
  // Create and Save a new Devices
  exports.create = (req, res) =&gt; {
    // Validate request
    if (!req.body.serialno) {
      res.status(400).send({
        message: &quot;Content can not be empty!&quot;
      });
      return;
    }
  
    // Create a Devices
    const device = {
      serialno: req.body.serialno,
      brand: req.body.brand,
      modelname: req.body.modelname 
    };
  
    // Save Devices in the database
    Device.create(device)
      .then(data =&gt; {
        res.send(data);
      })
      .catch(err =&gt; {
        res.status(500).send({
          message:
            err.message || &quot;Some error occurred while creating the Devices.&quot;
        });
      });
  };
  
  // Retrieve all Devicess from the database.
  exports.findAll = (req, res) =&gt; {
  
      const serialno = req.query.serialno;
      var condition = serialno ? { serialno: { [Op.like]: `%${serialno}%` } } : null;
    
      Device.findAll({ where: condition })
        .then(data =&gt; {
          res.send(data);
        })
        .catch(err =&gt; {
          res.status(500).send({
            message:
              err.message || &quot;Some error occurred while retrieving Devicess.&quot;
          });
        });
    
  };
  
  // Find a single Devices with an id
  exports.findOne = (req, res) =&gt; {
  
      const id = req.params.id;
  
      Device.findByPk(id)
        .then(data =&gt; {
          res.send(data);
        })
        .catch(err =&gt; {
          res.status(500).send({
            message: &quot;Error retrieving Devices with id=&quot; + id
          });
        });
    
  };
  
  // Update a Devices by the id in the request
  exports.update = (req, res) =&gt; {
      const id = req.params.id;
  
    Device.update(req.body, {
      where: { id: id }
    })
      .then(num =&gt; {
        if (num == 1) {
          res.send({
            message: &quot;Devices was updated successfully.&quot;
          });
        } else {
          res.send({
            message: `Cannot update Devices with id=${id}. Maybe Devices was not found or req.body is empty!`
          });
        }
      })
      .catch(err =&gt; {
        res.status(500).send({
          message: &quot;Error updating Devices with id=&quot; + id
        });
      });
  };
  
  // Delete a Devices with the specified id in the request
  exports.delete = (req, res) =&gt; {
  
      const id = req.params.id;
  
      Device.destroy({
        where: { id: id }
      })
        .then(num =&gt; {
          if (num == 1) {
            res.send({
              message: &quot;Devices was deleted successfully!&quot;
            });
          } else {
            res.send({
              message: `Cannot delete Devices with id=${id}. Maybe Devices was not found!`
            });
          }
        })
        .catch(err =&gt; {
          res.status(500).send({
            message: &quot;Could not delete Devices with id=&quot; + id
          });
        });
    
  };
  
  // Delete all Devicess from the database.
  exports.deleteAll = (req, res) =&gt; {
      Device.destroy({
          where: {},
          truncate: false
        })
          .then(nums =&gt; {
            res.send({ message: `${nums} Devicess were deleted successfully!` });
          })
          .catch(err =&gt; {
            res.status(500).send({
              message:
                err.message || &quot;Some error occurred while removing all Devicess.&quot;
            });
          });
    
  };
  
  
  </code></pre>
  <p><code>postman</code> request</p>
  <pre><code>
  http://localhost:3000/api/devices
  
  {
      &quot;serialno&quot;:123,
      &quot;brand&quot;:&quot;apple&quot;,
      &quot;modelname&quot;:&quot;XS&quot;
  }
  </code></pre>
  <p><code>model.js</code></p>
  <pre><code>module.exports = (sequelize, Sequelize) =&gt; {
      const Device = sequelize.define(&quot;device&quot;, {
        serialno: {
          type: Sequelize.INTEGER
        },
        brand: {
          type: Sequelize.STRING
        },
        modelname: {
          type: Sequelize.STRING
        }
      });
    
      return Device;
    };
    
  </code></pre>
  <p>routes js</p>
  <pre><code>module.exports = app =&gt; {
      const devices = require(&quot;../controllers/controller.js&quot;);
    
      var router = require(&quot;express&quot;).Router();
    
      // Create a new devicesial
      router.post(&quot;/&quot;, devices.create);
    
      // Retrieve all devices
      router.get(&quot;/&quot;, devices.findAll);
    
      // Retrieve all published devices
  
    
      // Retrieve a single deviceswith id
      router.get(&quot;/:id&quot;, devices.findOne);
    
      // Update a deviceswith id
      router.put(&quot;/:id&quot;, devices.update);
    
      // Delete a deviceswith id
      router.delete(&quot;/:id&quot;, devices.delete);
    
      // Delete all devices
      router.delete(&quot;/&quot;, devices.deleteAll);
    
      app.use('/api/devices', router);
    };
  </code></pre>