I'm trying to learn use mongoose + mongoDB + next + express in test project. I have looked some solutions here (like Cannot overwrite model once compiled Mongoose and others like it), but it doen't work in my case.
For first start there is appear an error "User.find is not a function", and after reload server (by saving changes, for example) there is appear an error "Cannot overwrite users
model once compiled"
I use nvm 10.18.0, next 9.3.4, express 4.17.1, mongoose 5.9.7
Here's my files:
//server.js
const express = require('express');
const next = require('next');
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const mongoose = require('mongoose');
const cors = require('cors');
const morgan = require('morgan');
app.prepare()
.then(() => {
const server = express();
var mongoDB = 'mongodb://127.0.0.1:27017/admin';
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
server.use(morgan('combined'));
server.use(cors());
server.enable('trust proxy');
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`>>> [INFO-SERVER] Ready on http://localhost:${port}`);
});
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});
//./models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var SomeModelSchema = new Schema({
a_string: String,
a_date: Date
});
const User = mongoose.model('users', SomeModelSchema );
module.exports = User;
//./pages/index.js
const User = require('../models/user');
console.log(User.find({}))
const Home = () => (
<div className="container">
Hello World
</div>
)
export default Home
How can I solve these errors?
P.S. There is the errors screenshots: