User.find 不是函数,编译后无法覆盖“用户”模型

User.find is not a function and Cannot overwrite `users` model once compiled

我正在尝试学习在测试项目中使用 mongoose + mongoDB + next + express。我在这里查看了一些解决方案(例如 Cannot overwrite model once compiled Mongoose 和其他类似的解决方案),但它在我的情况下不起作用。

首次启动出现错误 "User.find is not a function",重新加载服务器后(例如通过保存更改)出现错误 "Cannot overwrite users model once compiled"

我用nvm 10.18.0,next 9.3.4,express 4.17.1,mongoose 5.9.7

这是我的文件:

//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

如何解决这些错误?

P.S。有错误截图:


同事帮我理解。无法在前端使用模型。如果像这样创建服务器端函数:

//./foo.js    
const User = require('./models/user');

const foo = () => {
    console.log(">>>>>>>>>>>>>>>>>>>>>",User.find({email: 'va@mail.com'}))
}

module.exports = foo;

像这样在服务器中导入并使用它:

//./server.js
const foo = require('./foo')

app.prepare()
  .then(() => {
    const server = express();
    var mongoDB = 'mongodb://127.0.0.1:27017/admin';
    mongoose.connect(mongoDB);
    var db = mongoose.connection;
    foo()
...
})

一切都会好的!

非常感谢您的帮助!