Next.js API 路由

Jos*_*iel 1 mongoose mongodb reactjs next.js

希望能得到熟悉 Next.js 的人的帮助。我在将我的快速 API 路由转换为页面中的 Next.js 内部 API 路由时遇到问题,这看起来确实很有希望。问题是它似乎不适用于我的猫鼬模型和方法。

例如:

不起作用:

 const doc = await UserModel.findOne({ email: 'josh.mcdaniel@gmail.com' })
Run Code Online (Sandbox Code Playgroud)

作品:

const doc = await req.db
    .collection('users')
    .findOne({ email: 'josh.mcdaniel@gmail.com' }) 
Run Code Online (Sandbox Code Playgroud)

不起作用:

const doc = await req.db
    .collection('users')
    .find()
Run Code Online (Sandbox Code Playgroud)

不确定我是否只是做错了或者我设置了不正确的东西。希望得到一些帮助。我的用户模型供参考:

const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema({
  fullName: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  userName: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  posts: {
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'foodPost' }],
  },
  saves: {
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'foodPost' }],
  },
  photo: {
    type: String,
    default: 'https://via.placeholder.com/400',
  },
  followingCount: {
    type: Number,
    default: 0,
  },
  followerCount: {
    type: Number,
    default: 0,
  },
  following: {
    type: Array,
    default: [],
  },
  followers: {
    type: Array,
    default: [],
  },
  startDate: {
    type: Date,
    default: Date.now(),
  },
  notifications: {
    type: Array,
    default: [],
  },
})
export default mongoose.models.user || mongoose.model('user', UserSchema)
Run Code Online (Sandbox Code Playgroud)

必须更改导出,以便它不再出现覆盖错误。

谢谢你!

Mat*_*tta 5

尝试这样做...(这篇文章有点长,但如果您遵循逻辑和导入顺序,它应该可以工作):

\n\n

项目结构示例:

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .next\n|\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 database\n|   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n|\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 models\n|   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 all.js\n|   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.js\n|   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 teams.js\n|\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n|   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 pages\n|       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 api\n|       |   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 teams\n|       |       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 names.js\n|       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n|\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 next.config.json\n
Run Code Online (Sandbox Code Playgroud)\n\n

database/index.js -- 这将使用mongoose建立mongo 数据库连接

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .next\n|\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 database\n|   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n|\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 models\n|   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 all.js\n|   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.js\n|   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 teams.js\n|\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n|   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 pages\n|       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 api\n|       |   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 teams\n|       |       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 names.js\n|       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n|\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 next.config.json\n
Run Code Online (Sandbox Code Playgroud)\n\n

models/all.js ——这将要求所有模型注册数据库连接

\n\n
require("./team");\nrequire("./user");\n...etc\n
Run Code Online (Sandbox Code Playgroud)\n\n

models/index.js ——这将导出mongoose 模型实例(您不需要此文件,因为您只需从文件中导入mongoose和检索该实例,但我喜欢减少重复导入)model(Team)

\n\n
const bluebird = require("bluebird");\nconst mongoose = require("mongoose");\n\n// I use process.env.DATABASE as a flexible database name\n// this can be set in an ".env" file or in your package.json scripts...\n// like "dev" : "DATABASE=example-dev next dev"\nconst { DATABASE } = process.env;\n\nconst options = {\n    useNewUrlParser: true, // avoids DeprecationWarning: current URL string parser is deprecated\n    useCreateIndex: true, // avoids DeprecationWarning: collection.ensureIndex is deprecated.\n    useFindAndModify: false, // avoids DeprecationWarning: collection.findAndModify is deprecated.\n    useUnifiedTopology: true, // avoids DeprecationWarning: current Server Discovery and Monitoring engine is deprecated\n};\n\n// connects to a mongodb database\nmongoose.connect(`mongodb://localhost/${DATABASE}`, options); \n\n// uses bluebird for mongoose promises\nmongoose.Promise = bluebird; \n
Run Code Online (Sandbox Code Playgroud)\n\n

models/team.js --这会将模式建立为猫鼬模型

\n\n
require("./team");\nrequire("./user");\n...etc\n
Run Code Online (Sandbox Code Playgroud)\n\n

pages/api/teams/all.js --从 API 路由中导入模型实例...

\n\n
const { model } = require("mongoose");\n\nmodule.exports = {\n    Team: model("Team"),\n    User: model("User"),\n    ...etc\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

next.config.js——这将在下次加载之前建立连接池并注册模型

\n\n
const { Schema, model } = require("mongoose");\n\nconst teamSchema = new Schema({\n    league: { type: String, required: true },\n    team: { type: String, unique: true },\n    name: { type: String, unique: true, lowercase: true },\n});\n\nmodule.exports = model("Team", teamSchema);\n
Run Code Online (Sandbox Code Playgroud)\n