OverwriteModelError:编译后无法覆盖“团队”模型

时间:2018-09-25 08:49:52

标签: javascript node.js mongodb mongoose

几个小时后。研究StackOverflow。关于此问题的文章很多。但是我无法弄清楚我在哪里错了。

我的代码:

models / Team.js

LI

models / Division.js

const mongoose = require('mongoose');

const { Schema } = mongoose;

const teamSchema = new Schema({
  name: { type: String, minLength: 1, required: true },
  description: { type: String },
});

mongoose.model('teams', teamSchema);

在index.js中需要我

const mongoose = require('mongoose');

const { Schema } = mongoose;

const divisionSchema = new Schema({
  name: { type: String, minLength: 1, required: true },
  description: { type: String },
});

mongoose.model('teams', divisionSchema);

routes / teamRoutes.js

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const keys = require('./config/keys');

require('./models/Team');
require('./models/Division');

mongoose.Promise = global.Promise;
mongoose.connect(keys.mongoURI, { useNewUrlParser: true });

const app = express();
app.use(bodyParser.json());

require('./routes/teamRoutes')(app);

const port = process.env.PORT || 5000;
app.listen(port);

如果我删除const mongoose = require('mongoose'); const Team = mongoose.model('teams'); module.exports = (app) => { app.get('/api/teams', async (req, res) => { const teamsList = await Team.find({}); res.send(teamsList); }); }; ,则可以。这是为什么? 如果没有,我得到了错误:

require('./models/Division');

1 个答案:

答案 0 :(得分:1)

mongoose.model('teams', divisionSchema);
我猜

在models / Division.js中,“团队”应该是“部门”