Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Cannot read properties of undefined "collectionName" - Mongoose.models

I'm trying to data in the Note collection from a local database of MongoDB using Mongoose with Nextjs and getting an error "Cannot read properties of undefined (reading 'note')"

Any ideas on what I am doing wrong?

import mongoose from 'mongoose';

const { Schema } = mongoose;
const NoteSchema = new Schema(
   {
      title: {
         type: String,
         maxlength: 60,
      },
      description: {
         type: String,
         maxlength: 200,
      },
   },
   { timestamps: true }
);

export default mongoose.models.note || mongoose.model('note', NoteSchema)

import dbConnect from "../../../lib/dbConnect"
import Note from "../../../models/Note"

export default async function handler(req, res) {
    const { method } = req
    
    await dbConnect()

    if (method === 'GET') {

        try {
            const note = await Note.find({}) 

            res.status(200).json({ success: true, data: note})

          } catch (error) {
            res.status(400).json({ success: false })
          }
        
    }

    if (method === 'POST') {

        try {

            const note = await Note.create(req.body)
            
            res.status(200).json({ success: true, data: note})
            
        } catch (error) {

            res.status(500).json(error)
            
        }
        
    }
}
import mongoose from 'mongoose'

const {MONGODB_URI} = process.env




let cached = global.mongoose

if (!cached) {
  cached = global.mongoose = { conn: null, promise: null }
}

async function dbConnect() {
  if (cached.conn) {
    return cached.conn
  }

  if (!cached.promise) {
    const opts = {
      useNewUrlParser: true,
      useUnifiedTopology:true,
      bufferCommands: false,
    }

    cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
      return mongoose
    })
  }
  cached.conn = await cached.promise
  return cached.conn
}

export default dbConnect

I get the following

Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'note')

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I was able to fix it by changing the export module

global.PizzaSchema = global.NoteSchema || mongoose.model('Note', NoteSchema);
export default global.NoteSchema;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!