Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.96k stars 3.84k forks source link

schema._preCompile is not a function with Version 6.xx #11346

Closed michelalbers closed 2 years ago

michelalbers commented 2 years ago

Do you want to request a feature or report a bug? bug

What is the current behavior? mongoose is listed as a peerDependency in my database package and as a dependency in my consuming api package. No duplicate versions of mongoose are present! yarn list --pattern mongoose only reveals version 6.2.0.

When starting up the API I receive the error TypeError: schema._preCompile is not a function

If the current behavior is a bug, please provide the steps to reproduce.

shared/database/model.ts

import * as mongoose from "mongoose";
import { EasyBillReceiptDocument } from "./easyBillReceipt";
import { IProfileDocument } from "./profile";
import { ProfilePayOutTransactionDocument } from "./profilePayOutTransaction";

export enum ProfilePayoutRequestStatus {
  CREATED = "created",
  APPROVED = "approved",
  REJECTED = "rejected",
  PAID = "paid",
}

export interface ProfilePayoutRequestDocument extends mongoose.Document {
  profile: string | mongoose.Types.ObjectId | IProfileDocument;
  createdAt: Date;
  updatedAt: Date;
  amount: number;
  from: Date;
  until: Date;
  calculatedAmount: () => Promise<number>;
  status: ProfilePayoutRequestStatus;
  receipt?: string | mongoose.Types.ObjectId | EasyBillReceiptDocument;
  profilePayoutTransaction?:
    | string
    | mongoose.Types.ObjectId
    | ProfilePayOutTransactionDocument;
}

export const profilePayoutRequestSchema = new mongoose.Schema(
  {
    profile: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Profile",
      required: true,
    },
    amount: {
      type: Number,
      required: true,
    },
    status: {
      type: String,
      enum: Object.values(ProfilePayoutRequestStatus),
      required: true,
    },
    receipt: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "EasyBillReceipt",
      required: false,
    },
    profilePayoutTransaction: {
      type: mongoose.Schema.Types,
      ref: "Transaction",
      required: false,
    },
    from: {
      type: Date,
      required: true,
    },
    until: {
      type: Date,
      required: true,
    },
  },
  {
    timestamps: true,
  }
);

export default mongoose.model<ProfilePayoutRequestDocument>(
  "ProfilePayoutRequest",
  profilePayoutRequestSchema
);

packages/api/updateModel.ts

import Model from '@shared/models';

const updateModel = async (instance: Model) => {
  instance.amount = 1000;
  await instance.save();
}

export default updateModel;

shared/models/tsconfig.json

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es5",
    "noImplicitAny": false,
    "module": "commonjs",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "outDir": "dist",
    "declaration": true,
    "lib": [
      "dom",
      "esnext"
    ],
    "typeRoots": [
      "./node_modules/@types",
      "../../node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "**/*/__tests__",
    "**/*/__tests__"
  ],
  "include": [
    "src"
  ]
}

packages/api/tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "preserveSymlinks": true,
    "resolveJsonModule": true,
    "allowUnreachableCode": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "target": "es6",
    "noImplicitAny": false,
    "skipLibCheck": true,
    "module": "commonjs",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "outDir": "./dist",
    "typeRoots": ["./node_modules/@types", "../../node_modules/@types"],
    "lib": [
      "dom",
      "dom.iterable",
      "scripthost",
      "es7",
      "es2020",
      "esnext.asynciterable"
    ]
  },
  "exclude": [
    "node_modules",
    "../../node_modules",
    "./src/profileImport.ts",
    "**/*/__tests__",
    "**/*/__tests__"
  ],
  "include": ["./src"]
}

What is the expected behavior? Should start up without errors

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Node: 14.15.4 Typescript: 4.5.5 Mongoose: 6.2.0

Output of yarn list --pattern mongoose: └─ mongoose@6.2.0

No duplicate versions of mongoose are present.

michelalbers commented 2 years ago

Found it ...

    profilePayoutTransaction: {
      type: mongoose.Schema.Types,
      ref: "Transaction",
      required: false,
    },

needed to be

    profilePayoutTransaction: {
      type: mongoose.Schema.Types.ObjectId, // <-- .ObjectId was missing
      ref: "Transaction",
      required: false,
    },