我尝试使用NextJS 13.4.12与mongoose 7.4.2我得到OverrideModelError有时当我从Web浏览器获取RestAPI。
我把这个项目上传到github:https://github.com/pzoli/homework4nextjs
My Difficulty. ts数据定义如下:
import mongoose, { Schema, model, Types } from "mongoose";
const difficultySchema = new Schema({
name: {
type: String,
required: true,
},
value: {
type: Number,
required: true,
},
});
export default mongoose.models.Difficulty || model("difficulty", difficultySchema);
我的dbConnect.ts是这样的:
import mongoose, { connect } from "mongoose";
let cached: undefined | Promise<void>;
export default () => {
if (mongoose.connections && mongoose.connections[0].readyState)
return Promise.resolve();
if (!cached) {
cached = connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/quiz')
.then(() => console.log("connected to database."))
.catch((err) => console.log("could not connect to database", err));
}
return cached;
}
难度目录中的. tsx页为:
"use client";
import React from 'react'
import { DataTable } from "primereact/datatable"
import { Column } from "primereact/column"
import { useQuery } from '@tanstack/react-query';
import Link from 'next/link';
export default function Difficulty() {
const { data: difficultyValues, status: dataFetchStatus, isLoading: isDataLoading } = useQuery({
queryKey: ["difficulty"],
queryFn: async () => {
const res = await fetch(`/api/difficulty`, { cache: "no-cache" });
return res.json();
}
});
return (
<>
<DataTable value={difficultyValues} loading={isDataLoading}>
<Column field='name' header="Name"></Column>
</DataTable>
<Link href="/">Home</Link>
</>
)
}
route. ts如下:
import Difficulty from "@/app/api/models/Difficulty";
import connection from "@/app/lib/dbConnect";
import { NextResponse } from "next/server";
export async function GET() {
await connection();
let result = await Difficulty.find({});
return NextResponse.json(result);
}
我的UserAnswer.ts代码如下(在错误消息中引用,但未使用任何route. ts):
import mongoose, { Schema, model, Types } from "mongoose";
const userAnswerSchema = new Schema({
question_answer_id: {
type: Types.ObjectId,
required: true,
ref: 'questionanswer'
},
recorded_time: {
type: Date,
required: false,
},
user_id: {
type: Types.ObjectId,
required: true,
ref: 'user'
},
thinkingtime: {
type: Number,
required: true,
},
});
export default mongoose.models.UserAnswer || model("useranswer", userAnswerSchema);;
整个错误消息是:
- [31merror [39m覆盖模型错误:编译后无法覆盖
useranswer
模型。在 Mongoose 型号(E:\work\integrity\Homeworks\homework4nextjs\node_modules\mongoose\lib\index.js:第五百六十三章:13)在eval(webpack-internal:///(rsc)/./app/API/models/UserAnswer。TS:28:184)在(rsc)/。/app/API/models/UserAnswer。ts(E:\work\integrity\Homeworks\homework4nextjs.next\server\app\api\question\route.js:八十二:1)atwebpack_require(E:\work\integrity\Homeworks\homework4nextjs. next\server\webpack-runtime。js:三十三:42)在eval(webpack-internal:///(rsc)/./app/API/models/models. TS:7:69)在(rsc)/。/app/API/models/models答案。ts(E:\work\integrity\Homeworks\homework4nextjs.next\server\app\api\question\route.js:七十二:1)atwebpack_require(E:\work\integrity\Homeworks\homework4nextjs. next\server\webpack-runtime。js:三十三:42)在eval(webpack-internal:/(rsc)/./app/API/models/Question. TS:7:73)在(rsc)/。/app/API/models/Question. ts(E:\work\integrity\Homeworks\homework4nextjs.next\server\app\api\question\route.js:六十二:1)atwebpack_require(E:\work\integrity\Homeworks\homework4nextjs. next\server\webpack-runtime。js:三十三:42)在eval(webpack-internal:/(rsc)/./app/API/question/route。ts:5:82)在(rsc)/。/app/API/question/route。ts(E:\work\integrity\Homeworks\homework4nextjs.next\server\app\api\question\route.js:九十二:1)atwebpack_require(E:\work\integrity\Homeworks\homework4nextjs. next\server\webpack-runtime。js:三十三:42)在eval(webpack-internal:/(rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader. js?name = app%2Fapi%2Fquestion%2Froute&page =%2Fapi%2Fquestion%2Froute&appPaths =& pagePath = private-next-app-dir % 2Fapi % 2Fquestion%2Froute. ts & appDir = E%3A%5Cwork%5Cintegrity%5CHomeworks%5Chomework4nextjs%5Capp&pageExtensions = tsx & pageExtensions = ts & pageExtensions = jsx & pageExtensions = js & rootDir = E%3A%5Cwork%5Cintegrity%5CHomeworks%5Chomework4nextjs&isDev = true & tsconfigPath = tsconfig。json & basePath =& assetPrefix =& nextConfigOutput =& preferredRegion =& middlewareConfig = e30%3D!:16:126)at(rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader. js?name = app%2Fapi%2Fquestion%2Froute&page =%2Fapi%2Fquestion%2Froute&appPaths =& pagePath = private-next-app-dir % 2Fapi % 2Fquestion%2Froute. ts & appDir = E%3A%5Cwork%5Cintegrity%5CHomeworks%5Chomework4nextjs%5Capp&pageExtensions = tsx & pageExtensions = ts & pageExtensions = jsx & pageExtensions = js & rootDir = E%3A%5Cwork%5Cintegrity%5CHomeworks%5Chomework4nextjs&isDev = true & tsconfigPath = tsconfig。json & basePath =& assetPrefix =& nextConfigOutput =& preferredRegion =& middlewareConfig = e30%3D!(E:\work\integrity\Homeworks\homework4nextjs.next\server\app\api\question\route.js:42:1)atwebpack_require(E:\work\integrity\Homeworks\homework4nextjs. next\server\webpack-runtime. js:33:42)atwebpack_exec(E:\work\integrity\Homeworks\homework4nextjs.next\server\app\api\question\route.js:692:39)at E:\work\integrity\Homeworks\homework4nextjs.next\server\app\api\question\route.js:693:28 at Object.(E:\work\integrity\Homeworks\homework4nextjs.next\server\app\api\question\route.js:第六百九十六章:(3)模块。_compile(e:\work\integrity\Homeworks\homework4nextjs\lib\internal\modules\cjs\loader.js:1255:14)at Module._extensions..js(e:\work\integrity\Homeworks\homework4nextjs\lib\internal\modules\cjs\loader.js:1309:(10)模块。负载(e:\work\integrity\Homeworks\homework4nextjs\lib\internal\modules\cjs\loader.js:1113:32)在模块。_load(e:\work\integrity\Homeworks\homework4nextjs\lib\internal\modules\cjs\loader.js:960:(12)模块。require(e:\work\integrity\Homeworks\homework4nextjs\lib\internal\modules\cjs\loader.js:一一三七:19)at require(e:\work\integrity\Homeworks\homework4nextjs\lib\internal\modules\helpers.js:一百二十一:18)按要求页(e:\work\integrity\Homeworks\homework4nextjs\node_modules\next\dist\server\require.js:一百一十二:75)在e:\work\integrity\Homeworks\homework4nextjs\node_modules\next\dist\server\load-components。js:八十:84在过程中processTicksAndRejections(e:\work\integrity\Homeworks\homework4nextjs\lib\internal\process\task_processes.js:九十五:5)在100c负载下,Impl(e:\work\integrity\Homeworks\homework4nextjs\node_modules\next\dist\server\load-components。js:八十:26)在DevServer上。findPageServiceImpl(E:\work\integrity\Homeworks\homework4nextjs\node_modules\next\dist\server\next-server。js:四百三十四:36){name:"OverwriteModelError",摘要:未定义,堆栈:'OverwriteModelError:无法覆盖useran…dules\next\dist\server\next-server.js:434:36)', message: 'Cannot overwrite
useranswer "model once compiled.",Symbol(NextjsError):服务器
我使用“npm run dev”命令运行项目。如果我运行“npm run build”和“npm run start”,错误就像我测试的那样消失了。错误不是第一次出现,有时需要刷新页面。请帮助我了解如何正确使用它。
1条答案
按热度按时间wfveoks01#
我测试了一个解决方案。只需将名称capital supercase更改为您的模型名称:
Camel 案:
错误消失。