简体   繁体   中英

Mongoose Error: Schema is not constructor

Here my Schema file is given below

const mongoose = require("mongoose");

const ProblemSchema = new mongoose.Schema(
  {
    slug: {
      type: String,
      required: true,
      unique: true,
    },
    title: {
      type: String,
      required: true,
    },
    desc: { type: String },
    input: { type: String, required: true },
    output: { type: String, required: true },
    constraints: { type: String },
    statement: { type: String, required: true },
    testcase: [
      {
        input: { type: String },
        output: { type: String },
        sample: { type: Boolean },
        explanation: { type: String },
      },
    ],
  },
  { timestamps: true }
);

module.exports = mongoose.model("Problem", ProblemSchema);

When I save data using :

const Problem = require("./models/Problem");
const newData = new Problem(data)
await newData.save()

I get the "Problem is not a constructor" error. How do I solve this issue? Thanks in advance.

Please import your model like this.

const Problem = require("./yourschema_file_path");

Not like this.

const { Problem } = require("./yourschema_file_path");

you dont have to destructure while import.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM