Hoping to get some help from someone who is familiar with Next.js. I am having an issue converting my express API routes to Next.js's internal API routes in pages which looks really promising. The issue is it doesn't seem to be working with my mongoose models and methods.
For example:
doesn't work:
const doc = await UserModel.findOne({ email: 'josh.mcdaniel@gmail.com' })
works:
const doc = await req.db
.collection('users')
.findOne({ email: 'josh.mcdaniel@gmail.com' })
doesn't work:
const doc = await req.db
.collection('users')
.find()
Not sure if I am just doing it wrong or I have something set up incorrectly. Hoping for some help. My user model for reference:
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)
Had to change the export so that it would stop giving an overwrite error.
Thank you!