In my models/user.js
file:
var mongoose = require(\'mongoose\');
var Schema = mongoose.Schema;
var userSchema = new Schema({
(define schema
I got the same problem when I am trying the MEAN tutorial.
After done a little bit research, I found that in app.js, if I put require("./models/User") before var routes = require("./routes/index"), then it works.
Like this:
mongoose.connect("mongodb://localhost/news");
require("./models/Posts");
require("./models/Comments");
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
Hope the answer will be helpful!
I encountered this issue when tried to add a new model on the base code of this tutorial User Auth on MEAN stack. The solution is like what Ahei mentioned.
Specifically, I need to add a line require('path/to/your/model')
in the end of /app_api/models/db.js
which is required in the app.js
. It is better to keep the consistency of the structure for the ease of development.
The problem arises in the require()
statements for the models. You need to move all the MongoDB dependencies above the routes dependencies like this eg
//blog.js file
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const blogSchema = new Schema({
title : String
});
mongoose.model('blogs', blogSchema);
in server.js
//server.js file
require('dotenv').config();
const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
require('./models/blog');
const Blog = mongoose.model('blogs');
const URL = process.env.MONGO_DB_URL;
mongoose.connect(URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection
.once('open', () => console.log('Connected to MongoLab instance.'))
.on('error', (error) => console.log('Error connecting to MongoLab:', error));
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', (req,res)=>{
res.send('Hello World')
});
app.post('/blog',async (req, res) =>{
const blog = await new Blog(req.body).save();
res.send('just an example');
});
This problem occurred when try to get model before load the model file load
I solved same problem in my mean.io project
In controller:
'use strict';
require('../models/settingsModel'); // load settingsModel.js file before get mongoose.model('Settings')
var mongoose = require('mongoose'),
Settings = mongoose.model('Settings'),
Q = require('q');
I tried all the above solutions but all of them failed instead I found that the solution was to clear my database (MongoDB) and then rerunning the app
I got this error while trying to use mongoose-fixture
to seed some default data into a mongo collection. Was baffled for long, following this and similar threads for help, trying to debug. Ultimately the issue turned out to be due to mongoose
and mongoose-fixture
versions in my case.
If no code changes help, or if you get this error while trying to correctly use mongoose-fixture
(which is supposed to register your schemas for you), try this. Delete the node_modules
dir of your project, run an npm cache clean
, and then an npm install
.
If even this doesn't help, try comparing versions of mongoose
/ mongoose-fixture
between the problematic app and one that works, and try changing the version in your package.json
, and repeating the above steps. This worked for me.