[英]Mongoose Error: Schema is not constructor
下面是我的架構文件
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);
當我使用以下方式保存數據時:
const Problem = require("./models/Problem");
const newData = new Problem(data)
await newData.save()
我收到“問題不是構造函數”錯誤。 我該如何解決這個問題? 提前致謝。
請像這樣導入您的模型。
const Problem = require("./yourschema_file_path");
不是這樣的。
const { 問題 } = require("./yourschema_file_path");
您不必在導入時進行解構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.