Home > Blockchain >  TypeError: Cannot read property 'create' of undefined error
TypeError: Cannot read property 'create' of undefined error

Time:10-10

Hi I am creating NodeJS service with postgreSQL. When I trying to test POST method using postman. I got following error. Can you please help me to solve this.

TypeError: Cannot read property 'create' of undefined
at exports.create (D:\MyWork\Team-7\Backend\MicroServices\booking-service\app\controllers\booking.controller.js:23:12)
at Layer.handle [as handle_request] (D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\layer.js:95:5)
at next (D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\layer.js:95:5)
at D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\index.js:335:12)
at next (D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\index.js:174:3)
at router (D:\MyWork\Team-7\Backend\MicroServices\booking-service\node_modules\express\lib\router\index.js:47:12)

controller.js

const db = require("../models/booking.model");
const Tutorial = db.tutorial;
const Op = db.Sequelize;

  // Create a Tutorial
  const tutorial = {
    title: req.body.title,
    description: req.body.description,
    published: req.body.published ? req.body.published : false
  };

  // Save Tutorial in the database
  Tutorial.create(tutorial)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
    
    });
};

CodePudding user response:

That means the constant is Tutorial so db has not tutorial as a property.

CodePudding user response:

first check that if your migration has been run or not , then check that you export tutorial model correctly and try to import that like this

const db = require("../models");//if have index.js in models directory
const Tutorial = db.tutorial;
  • Related