gpt4 book ai didi

mongodb - 现在 Mongoose 无法在编译后覆盖模型

转载 作者:行者123 更新时间:2023-12-05 08:14:29 28 4
gpt4 key购买 nike

我正在使用 MongoDB Atlas 来托管数据库并使用这个无服务器函数查询数据:

import { NextApiRequest, NextApiResponse } from "next";

// models
import { Section } from "../../../models/Section";

// db connection
import { useDatabase } from "../../../middleware/useDatabase";

async function find(req: NextApiRequest, res: NextApiResponse) {
const { content } = req.query;
const response = await Section.find({ contentType: content });
res.send(response);
}

export default useDatabase(find);

中间件useDatabase配置如下:

import * as mongoose from "mongoose";
import { NextApiRequest, NextApiResponse } from "next";

export const useDatabase = handler => async (req: NextApiRequest, res: NextApiResponse) => {
if (mongoose.connections[0].readyState) return handler(req, res);
// Using new database connection
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
});

return handler(req, res);
}

我第一次执行查询并获取 api,效果很好,然后如果我编辑一些代码,保存它并 now dev 重新编译代码,我在这样做时遇到了这个错误再次查询:

MongooseError [OverwriteModelError]: Cannot overwrite `Section` model once compiled.
at new OverwriteModelError (/home/andres/Documents/shooter-app/node_modules/mongoose/lib/error/overwriteModel.js:20:11)
at Mongoose.model (/home/andres/Documents/shooter-app/node_modules/mongoose/lib/index.js:521:13)
at Module../models/Section.ts (/home/andres/Documents/shooter-app/.next/server/static/development/pages/api/sections/[content].js:154:63)
at __webpack_require__ (/home/andres/Documents/shooter-app/.next/server/static/development/pages/api/sections/[content].js:23:31)
at Module../pages/api/sections/[content].ts (/home/andres/Documents/shooter-app/.next/server/static/development/pages/api/sections/[content].js:198:73)
at __webpack_require__ (/home/andres/Documents/shooter-app/.next/server/static/development/pages/api/sections/[content].js:23:31)
at Object.3 (/home/andres/Documents/shooter-app/.next/server/static/development/pages/api/sections/[content].js:233:18)
at __webpack_require__ (/home/andres/Documents/shooter-app/.next/server/static/development/pages/api/sections/[content].js:23:31)
at /home/andres/Documents/shooter-app/.next/server/static/development/pages/api/sections/[content].js:91:18
at Object.<anonymous> (/home/andres/Documents/shooter-app/.next/server/static/development/pages/api/sections/[content].js:94:10)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Module.require (internal/modules/cjs/loader.js:848:19)
at require (internal/modules/cjs/helpers.js:74:18)
at DevServer.handleApiRequest (/home/andres/Documents/shooter-app/node_modules/next/dist/next-server/server/next-server.js:420:28)
at async Object.fn (/home/andres/Documents/shooter-app/node_modules/next/dist/next-server/server/next-server.js:350:37)
at async Router.execute (/home/andres/Documents/shooter-app/node_modules/next/dist/next-server/server/router.js:42:32)
at async DevServer.run (/home/andres/Documents/shooter-app/node_modules/next/dist/next-server/server/next-server.js:468:29) {
message: 'Cannot overwrite `Section` model once compiled.',
name: 'OverwriteModelError'
}

我该如何解决?谢谢!

最佳答案

当开发服务器在热模块替换期间尝试重新编译您的模型时,就会发生这种情况。

您可以通过更改代码以在导出模型时检查模型是否已编译来防止这种情况发生。

export default mongoose.models['Section'] || mongoose.model('Section', SectionSchema);

关于mongodb - 现在 Mongoose 无法在编译后覆盖模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851947/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com