mongoose 在我写了一个tslog.json之后,我得到了错误- OverwriteModelError:编译后无法覆盖`ExampleModel`模型

cwdobuhd  于 2023-10-19  发布在  Go
关注(0)|答案(1)|浏览(126)

我必须将我的NodeJS项目转换为typescript。我希望typescript和JavaScript能够顺利地一起工作,因为我想慢慢地进行转换,而不是一次完成。到目前为止,我的项目一直运作良好。只要我创建一个tslog.json文件,然后运行命令npm run build,它的构建就成功了。在运行npm run dev命令后,我得到错误

OverwriteModelError: Cannot overwrite BusDayWiseBreakDown model once compiled.

我还没有创建任何.ts文件。如何解决这个错误,在tslog.json中是否需要任何修改,或者这些是任何其他问题?
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"
  ]
}

我的package.json有这些构建和运行命令- 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",
我在BusDayWiseBreakDown模型上得到一个错误,所以我也发送该文件

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;
pbgvytdp

pbgvytdp1#

使用

const BusDayWiseBreakDown = mongoose.models.BusDayWiseBreakDown || mongoose.model('BusDayWiseBreakDown', BusDayWiseBreakDownSchema);

相关问题