导入Mongoose模式到我的NextJS页面

huangapple go评论102阅读模式
英文:

Importing Mongoose Schemas in my NextJS page

问题

我特别新手使用NextJS,想知道我能否请求一点关于我遇到问题的帮助。

我定义了两个Mongoose模式,并且已经在我的页面中使用了一个模式,没有任何问题。然而,一旦我导入第二个模式,我的页面就会拒绝渲染,并显示附带的错误。

无论我导入哪个模式,一旦导入第二个模式,第二个模式就会出现相同的错误。

以下是每个模式:

User.js

  1. import mongoose from "mongoose";
  2. const UserSchema = new mongoose.Schema({}, { strict: false });
  3. export default mongoose.models.User || mongoose.model("User", UserSchema);

Album.js

  1. import mongoose from "mongoose";
  2. const AlbumSchema = new mongoose.Schema({}, { strict: false });
  3. export default mongoose.models.Album || mongoose.model("Album", AlbumSchema);

这是发生错误的页面。

albums.js

  1. import Layout from "@/Components/Dashboard/DashLayout";
  2. import AlbumCard from "@/Components/Dashboard/Albums/AlbumCard";
  3. import AlbumSchema from "@/models/Album";
  4. import dbConnect from "@/utils/dcConnect";
  5. export default function DashAlbums({ albums }) {
  6. return (
  7. // 页面内容因导入而被省略
  8. );
  9. }
  10. // 需要身份验证
  11. DashAlbums.auth = true;
  12. export async function getServerSideProps(context) {
  13. dbConnect();
  14. const albums = await AlbumSchema.find()
  15. const data = albums.map(album => {
  16. return { _id: album._id, name: album.name, cover: album.cover }
  17. });
  18. return { props: { albums: JSON.parse(JSON.stringify(data)) } }
  19. }

请问有人能帮我找出问题出在哪里吗?

我尝试只导出模型,但是NextJS不喜欢那样,而我又不够了解,无法找出问题所在。

英文:

I'm particularly new to NextJS, and I was wondering if I could ask for a bit of help with an issue that I am having.

I have 2 Mongoose schemas defined and have been using one schema in my page for a while with no issues. However, as soon as I import the 2nd schema, my page refuses to render with the attached error.

It doesn't matter which schema I import, but as soon as a second schema is imported, I get the same error for the second schema.

Here is each schema:

User.js

  1. import mongoose from "mongoose";
  2. const UserSchema = new mongoose.Schema({}, { strict: false });
  3. export default mongoose.models.User || mongoose.model("User", UserSchema);

Album.js

  1. import mongoose from "mongoose";
  2. const AlbumSchema = new mongoose.Schema({}, { strict: false });
  3. export default mongoose.models.Album || mongoose.model("Album", AlbumSchema);

And here is the page in which the error is occurring.

albums.js

  1. import Layout from "@/Components/Dashboard/DashLayout";
  2. import AlbumCard from "@/Components/Dashboard/Albums/AlbumCard";
  3. import AlbumSchema from "@/models/Album";
  4. import dbConnect from "@/utils/dcConnect";
  5. export default function DashAlbums({ albums }) {
  6. return (
  7. // Page content removed for import
  8. );
  9. }
  10. // Require authentication
  11. DashAlbums.auth = true;
  12. export async function getServerSideProps(context) {
  13. dbConnect();
  14. const albums = await AlbumSchema.find()
  15. const data = albums.map(album => {
  16. return { _id: album._id, name: album.name, cover: album.cover }
  17. });
  18. return { props: { albums: JSON.parse(JSON.stringify(data)) } }
  19. }

Please could someone help me figure out where I have gone wrong?

I've tried only exporting the model, but NextJS doesn't like that and I don't have enough knowledge to figure out where I have gone wrong.

答案1

得分: 2

我认为问题出在你无法将Mongoose模式代码导入客户端组件中。

你需要编写一个API路由,在API处理程序中编写这个逻辑:

  1. dbConnect();
  2. const albums = await AlbumSchema.find()
  3. const data = albums.map(album => {
  4. return { _id: album._id, name: album.name, cover: album.cover }
  5. });

然后在getServerSideProps中向该API路径发出请求,并在props对象中返回正确的结果。

英文:

I think the problem is you cannot import the Mongoose schema code into the client components.

You have to write an API route, write this logic in api handler

  1. dbConnect();
  2. const albums = await AlbumSchema.find()
  3. const data = albums.map(album => {
  4. return { _id: album._id, name: album.name, cover: album.cover }
  5. });

then in getServerSideProps you make a request to that api path and return the correct result inside props object

huangapple
  • 本文由 发表于 2023年5月7日 01:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定