【发布时间】:2020-08-12 23:57:22
【问题描述】:
希望能从熟悉 Next.js 的人那里得到一些帮助。我在将我的快速 API 路由转换为 Next.js 的内部 API 路由时遇到问题,这看起来很有希望。问题是它似乎不适用于我的猫鼬模型和方法。
例如:
不起作用:
const doc = await UserModel.findOne({ email: 'josh.mcdaniel@gmail.com' })
作品:
const doc = await req.db
.collection('users')
.findOne({ email: 'josh.mcdaniel@gmail.com' })
不起作用:
const doc = await req.db
.collection('users')
.find()
不确定是我做错了还是我的某些设置不正确。希望得到一些帮助。 我的用户模型供参考:
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)
必须更改导出,以便它停止给出覆盖错误。
谢谢!
【问题讨论】:
标签: reactjs mongodb mongoose next.js