英文:
Importing Mongoose Schemas in my NextJS page
问题
我特别新手使用NextJS,想知道我能否请求一点关于我遇到问题的帮助。
我定义了两个Mongoose模式,并且已经在我的页面中使用了一个模式,没有任何问题。然而,一旦我导入第二个模式,我的页面就会拒绝渲染,并显示附带的错误。
无论我导入哪个模式,一旦导入第二个模式,第二个模式就会出现相同的错误。
以下是每个模式:
User.js
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({}, { strict: false });
export default mongoose.models.User || mongoose.model("User", UserSchema);
Album.js
import mongoose from "mongoose";
const AlbumSchema = new mongoose.Schema({}, { strict: false });
export default mongoose.models.Album || mongoose.model("Album", AlbumSchema);
这是发生错误的页面。
albums.js
import Layout from "@/Components/Dashboard/DashLayout";
import AlbumCard from "@/Components/Dashboard/Albums/AlbumCard";
import AlbumSchema from "@/models/Album";
import dbConnect from "@/utils/dcConnect";
export default function DashAlbums({ albums }) {
return (
// 页面内容因导入而被省略
);
}
// 需要身份验证
DashAlbums.auth = true;
export async function getServerSideProps(context) {
dbConnect();
const albums = await AlbumSchema.find()
const data = albums.map(album => {
return { _id: album._id, name: album.name, cover: album.cover }
});
return { props: { albums: JSON.parse(JSON.stringify(data)) } }
}
请问有人能帮我找出问题出在哪里吗?
我尝试只导出模型,但是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
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({}, { strict: false });
export default mongoose.models.User || mongoose.model("User", UserSchema);
Album.js
import mongoose from "mongoose";
const AlbumSchema = new mongoose.Schema({}, { strict: false });
export default mongoose.models.Album || mongoose.model("Album", AlbumSchema);
And here is the page in which the error is occurring.
albums.js
import Layout from "@/Components/Dashboard/DashLayout";
import AlbumCard from "@/Components/Dashboard/Albums/AlbumCard";
import AlbumSchema from "@/models/Album";
import dbConnect from "@/utils/dcConnect";
export default function DashAlbums({ albums }) {
return (
// Page content removed for import
);
}
// Require authentication
DashAlbums.auth = true;
export async function getServerSideProps(context) {
dbConnect();
const albums = await AlbumSchema.find()
const data = albums.map(album => {
return { _id: album._id, name: album.name, cover: album.cover }
});
return { props: { albums: JSON.parse(JSON.stringify(data)) } }
}
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处理程序中编写这个逻辑:
dbConnect();
const albums = await AlbumSchema.find()
const data = albums.map(album => {
return { _id: album._id, name: album.name, cover: album.cover }
});
然后在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
dbConnect();
const albums = await AlbumSchema.find()
const data = albums.map(album => {
return { _id: album._id, name: album.name, cover: album.cover }
});
then in getServerSideProps
you make a request to that api path and return the correct result inside props
object
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论