我正在写一些node.js / express app,我收到了这条消息:
抛出新的mongoose.Error.MissingSchemaError(name);
MissingSchemaError:尚未为模型“./models/User”注册架构。
我使用 express-generator 生成了我的项目,我的项目树看起来像这样:
app.js
仓/
node_modules /
的package.json
公共/
路由/
视图/
模型/
我在 models / 文件夹中创建了四个模型: 的 user.js的
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
email: String,
hash: String,
salt: String,
first_name: String,
last_name: String,
position: String
});
module.exports = mongoose.model('User', UserSchema);
payment.js
var mongoose = require('mongoose');
require('./user');
var PaymentSchema = new mongoose.Schema({
bookedDate: Date,
salary: {type: Number, default: 0},
employee: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
});
module.exports = mongoose.model('Payment', PaymentSchema);
task.js
var mongoose = require('mongoose');
require('./user');
var TaskSchema = new mongoose.Schema({
job: String,
employee: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
estimatedTime: { type: Number, default:0 },
hoursLogged: { type: Number, default:0 }
});
module.exports = mongoose.model('Task', TaskSchema);
vacation.js
var mongoose = require('mongoose');
require('./user');
var VacationSchema = new mongoose.Schema({
startDate: Date,
endDate: Date,
title: String,
employee: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
});
module.exports = mongoose.model('Vacation', VacationSchema);
我的错误出现在文件: routes \ index.js ,它以加载模型开头:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
require('../models/user');
require('../models/task');
require('../models/payment');
require('../models/vacation');
var User = mongoose.model('./models/User'); // <-- error throws here
var Task = mongoose.model('./models/Task');
var Vacation = mongoose.model('./models/Vacation');
var Payment = mongoose.model('./models/Payment');
但是我加载的第一个文件是 app.js ,它以:
开头var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
var mongoose = require('mongoose');
require('./models/user');
require('./models/task');
require('./models/payment');
require('./models/vacation');
var routes = require('./routes/index'); //<-- index.js with error loads here
var users = require('./routes/users');
mongoose.connect('mongodb://localhost/local_test');
有什么建议吗?
答案 0 :(得分:0)
更改
var mongoose = require('mongoose');
require('../models/user');
require('../models/task');
require('../models/payment');
require('../models/vacation');
var User = mongoose.model('./models/User'); // <-- error throws here
var Task = mongoose.model('./models/Task');
var Vacation = mongoose.model('./models/Vacation');
var Payment = mongoose.model('./models/Payment');
到
var mongoose = require('mongoose');
var User = require('../models/user');
var Task = require('../models/task');
var Vacation = require('../models/payment');
var Payment = require('../models/vacation');