0

I have to convert my NodeJS project into typescript.I want both typescript and javascript to work seemlessly together as I want to make the conversion slowly, not all at once. My project has been working fine up till now. As soon as I create a tsconfig.json file and then I run command npm run build, its builds successfully. After I run the command npm run dev I get the error

OverwriteModelError: Cannot overwrite BusDayWiseBreakDown model once compiled. 

I have not yet created any .ts file. How to resolve this error, are there any modifications needed in tsconfig.json or these is any other problem?

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "rootDir": "./src/",
    "moduleResolution": "node",
    "typeRoots": [
      "./node_modules/@types",
      "./src/types"
    ],
    "allowJs": true,
    "checkJs": false,
    "outDir": "./built",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true
  },
  "include": ["./src/**/*"],
  "exclude": [
    "node_modules",
    "node_modules/**/*",
    "./node_modules",
    "./node_modules/*",
    "./node_modules/@types/node/index.d.ts"
  ]
}

My package.json have these build and run commands - Build - "build": "tsc -p tsconfig.json". DEV - "dev": "cross-env NODE_ENV=development LOGGLY_ACTIVE=false ts-node-dev --watch ./src -P tsconfig.json built/index.js",

I am getting an error on BusDayWiseBreakDown model so I am sending that file as well

const mongoose = require('mongoose');
const { toJSON } = require('../../utils/mongoPlugins');

const BusDayWiseBreakDownSchema = mongoose.Schema(
  {
    busNumber: { type: String },
    cityId: { type: String },
    agencyId: { type: String },
    afcsAccountId: { type: String },
    agencyName: { type: String },
    depotName: { type: String },
    afcsAccountName: { type: String },
    operatorEmailId: { type: String },
    depotId: { type: String },
    operatorId: { type: String },
    operatorName: { type: String },
    totalIssueCardCashAmt: { type: Number },
    waybillNumber: { type: Number },
    waybillStatus: { type: String },
    totalRechargeCashAmt: { type: Number },
    totalIssueTicketByCash: { type: Number },
    totalPassIssueCashAmt: { type: Number },
    totalCardCollection: { type: Number },
    travelDate: { type: Number },
    ticketByPrepaidCard: { type: Number },
    ticketByPass: { type: Number },
    etimCash: { type: Number },
    driverSalary: { type: Number },
    conductorSalary: { type: Number },
    closureTime: { type: Date } // for running waybills it is the running time
  },
  {
    timestamps: true,
    collection: 'BusDayWiseBreakDown',
  },
);

BusDayWiseBreakDownSchema.index({ busNumber: 1 });

// add plugin that converts mongoose to json
BusDayWiseBreakDownSchema.plugin(toJSON);

/**
 * @typedef BusDayWiseBreakDown
 */
const BusDayWiseBreakDown = mongoose.model('BusDayWiseBreakDown', BusDayWiseBreakDownSchema);

module.exports =  BusDayWiseBreakDown;
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Roomi
  • 139
  • 8
  • Does this answer your question? [Cannot overwrite model once compiled Mongoose](https://stackoverflow.com/questions/19051041/cannot-overwrite-model-once-compiled-mongoose) – Matthieu Riegler Aug 24 '23 at 07:15
  • No, I checked out this question. This doesnt solve my issue – Roomi Aug 24 '23 at 07:15
  • This is different from the other question majorly because this issue is only occurring when I am trying typescript compiling – Roomi Aug 24 '23 at 07:18

1 Answers1

-1

Use

const BusDayWiseBreakDown = mongoose.models.BusDayWiseBreakDown || mongoose.model('BusDayWiseBreakDown', BusDayWiseBreakDownSchema);
Roomi
  • 139
  • 8