Why not able to create records in sequelize models?

39 Views Asked by At

I am using sequelize models and created City model using the sequelize-cli. The model and table is getting created after applying the migration. I have also created a repository for creating records in the City table.But while creating new records using this repository, I am getting the following error:

TypeError: City.create is not a function
    at CityRepo.createCity (C:\Users\DELL\Desktop\AirlineManagementProject\FlightsAndSearch\src\repository\city-repo.js:5:36)
    at Server.<anonymous> (C:\Users\DELL\Desktop\AirlineManagementProject\FlightsAndSearch\src\index.js:19:18)
    at Object.onceWrapper (node:events:627:28)
    at Server.emit (node:events:513:28)
    at emitListeningNT (node:net:1519:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21)

The code for CityRepo(repository for creating records in CIty table) :

const City =  require('../models/index');
class CityRepo{
    async  createCity({name}){
        try{
            const city= await City.create({name})
            return city;
        }
        catch(error){
            console.log(error);
        }
    }

    async  deleteCity(name){
        try{
            await City.deleteCity({name});
        }
        catch(error){
            console.log(error)
        }
    }
}

module.exports = CityRepo;

The city models file as generated by sequelize db:create command is :

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class City extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }
  City.init({
    name: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'City',
  });
  return City;
};

The code in migrations file for City model is :

'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Cities', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      name: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('Cities');
  }
};

Why is the record not getting created when I create it when the server is up:

const express = require("express");
const bodyParser = require('body-parser');
const CityRepo=require('./repository/city-repo');




const setupAndStartServer = async()=>{
    //create express object
    const app = express();
    const PORT = 3000;
    // Middleware to parse JSON and URL-encoded request bodies
    app.use(express.json());
    app.use(express.urlencoded({ extended: true }));

    app.listen(3000,()=>{
        console.log(`Server started at ${PORT}`);
        const cityRepo=new CityRepo();
        cityRepo.createCity({name:"New Delh"});
    })
}

setupAndStartServer();
0

There are 0 best solutions below