The insert of the Node express with problem

2

I'm new as a Node Express programmer with Mysql, I created the implementation to save, however he created the registry with null in the table, see how I performed the test in postman;

This is my entity;

module.exports = (sequelize, Sequelize) => {
    const Customer = sequelize.define('customer', {
      firstname: {
        type: Sequelize.STRING
      },
      lastname: {
        type: Sequelize.STRING
      },
      age: {
          type: Sequelize.INTEGER
      }
    });

    return Customer;
}

This is the method responsible for saving;

const db = require('../config/db.config.js');
const Customer = db.customers;

// Post a Customer
exports.create = (req, res) => {    
    // Save to MySQL database
    Customer.create({  
      firstname: req.body.firstname,
      lastname: req.body.lastname,
      age: req.body.age
    }).then(customer => {       
        // Send created customer to client
        res.send(customer);
    });
};

What can be wrong?

UPDATE =========================================

Route file;

module.exports = function(app) {

    const customers = require('../controller/customer.controller.js');

    // Create a new Customer
    app.post('/api/customers', customers.create);

    // Retrieve all Customer
    app.get('/api/customers', customers.findAll);

    // Retrieve a single Customer by Id
    app.get('/api/customers/:customerId', customers.findById);

    // Update a Customer with Id
    app.put('/api/customers/:customerId', customers.update);

    // Delete a Customer with Id
    app.delete('/api/customers/:customerId', customers.delete);
}

===================================================== ========

You have generated this error;

C:\SQL\api1>node server.js
internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module '../controllers/customer.controller'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\SQL\api1\app\route\customer.route.js:2:18)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\SQL\api1\server.js:5:20)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:236:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)

===========================================

Another error message;

node server.js
C:\SQL\api1\server.js:15
app.use('/api/customers', routes);
                          ^

ReferenceError: routes is not defined
    at Object.<anonymous> (C:\SQL\api1\server.js:15:27)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:236:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)
    
asked by anonymous 30.07.2018 / 13:39

1 answer

2

The first problem occurs that the type of body that you are using in Postman is incorrect. Use x-www-form-urlencoded :

By checking the code you provided in your repository, I suggest the following changes:

No server.js :

const express    = require('express');
const app        = express();
const bodyParser = require('body-parser');
const db         = require('./app/config/db.config.js');
const customer   = require('./app/route/customer.route');

// force: true will drop the table if it already exists
db.sequelize.sync({force: true}).then(() => {
  console.log('Drop and Resync with { force: true }');
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Registra a rota /api/customer para as rotas descritas em ./app/route/customer.route.js
app.use('/api/customers', customer);

// Create a Server
var server = app.listen(3000, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("App listening at http://%s:%s", host, port)
});

The route file will look as follows ( app/route/customer.route.js ):

const customer = require('../controller/customer.controller');
const express  = require('express');

const router = express.Router();

router.post('/', customer.create);

module.exports = router;

In turn, controller ( app/controller/customer.controller.js ):

const db = require('../config/db.config.js');
const Customer = db.customers;

async function create(req, res) {
  try {
    req.send(await _save(req.body));
  } catch(e) {
    res.status(500).send(e);
  }
}

async function _save(customer) {
  return Customer.create({  
    firstname: customer.firstname,
    lastname:  customer.lastname,
    age:       customer.age
  });
}

module.exports = {
  create
}
    
30.07.2018 / 14:40