【问题标题】:Now Mongoose Cannot overwrite model once compiled现在 Mongoose 编译后无法覆盖模型
【发布时间】:2020-05-08 03:42:18
【问题描述】:

我正在使用 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'
}

我该如何解决?谢谢!

【问题讨论】:

  • 我遇到了同样的问题,而且似乎是 Next.js 特有的。你有想过解决办法吗?

标签: mongodb mongoose next.js vercel


【解决方案1】:

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

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

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

【讨论】:

  • 这应该得到比它更多的支持。谢谢。
  • 我在为我的 API 创建测试文件时遇到了这个问题。事实是该文件之前已在另一个模块中导入,因此出现错误,因此进行检查解决了它。非常感谢!!!!
  • 这里唯一的缺点是尽管 Nextjs 团队有一个很好的例子 (github.com/vercel/next.js/blob/canary/examples/…) mongoose.models 不在他们的 TypeScript 定义中。
  • 对于遇到此问题的任何人:此方法适用于 Next 中的热模块替换。但是,您会在静力学上遇到错误。至少这对我来说是这样。我们正在检查它是否存在的模型没有显示为具有静态函数的扩展模型。因此,它将您的静力学显示为模型上不存在。我敢肯定,有更多知识的人可以/将对此进行扩展,但值得注意的是,您的静态数据并没有错。
猜你喜欢
  • 2013-10-03
  • 2015-04-25
  • 2021-05-18
  • 2021-07-06
  • 2021-03-01
  • 2020-10-07
  • 2019-01-13
  • 2014-08-03
相关资源
最近更新 更多