Home > Net >  Sequelize create my database but doesn't send files into that
Sequelize create my database but doesn't send files into that

Time:02-13

I'm trying to create a backend using express and sequelize, I have configured the config, controllers, models, routes and server files, for the first test the database was created successfully with two tables (products and reviews tables) now I'm trying to test the model and controller created with sequelize, but right when I try to use the post route to send the information inside the products table:

const db = require('../models');

// create main model
const Product = db.products;
const Review = db.reviews;

// main work

// 1. create product

const addProduct = async (req, res) => {
  let info = {
    title: req.body.title,
    price: req.body.price,
    description: req.body.description,
    published: req.body.published ? req.body.published : false
  }

  const product = await Product.create(info)
  res.status(200).send(product)
  console.log(product)
}

module.exports = {
  addProduct,
}\

, from the controllers folder, getting the information from the Database and the models folder:

module.exports = (sequelize, DataTypes) => {
  const Product = sequelize.define("product", {
    title: {
      type: DataTypes.TEXT,
      allowNull: false
    },
    price: {
      type: DataTypes.FLOAT,
      allowNull: false
    },
    description: {
      type: DataTypes.TEXT
    },
    published: {
      type: DataTypes.BOOLEAN
    },
  });

  return Product;
}

I get an error message on the terminal that says:

nodejs\node-fullApi\controllers\productController.js:13
    title: req.body.title,
                    ^

TypeError: Cannot read properties of undefined (reading 'title')
    at addProduct (F:\JavaScript\nodejs\node-fullApi\controllers\productController.js:13:21)
    at Layer.handle [as handle_request] (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\layer.js:95:5)
    at next (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\layer.js:95:5)
    at F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:341:12)
    at next (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:174:3)
    at router (F:\JavaScript\nodejs\node-fullApi\node_modules\express\lib\router\index.js:47:12)
[nodemon] app crashed - waiting for file changes before starting...

I tried to review everything I've done, I changed the Datatypes of the title from STRING to TEXT, but nothing worked and I keep getting this same error.

Does anyone have any idea of what can be so wrong there? My full project can be found on github following this link: My Git Repository

CodePudding user response:

You need to register all body parsers BEFORE registering any routers that may use a request body of a given format:

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const router = require('../routes/productRouter');
app.use('/api/products', router)
  • Related