mongoose使用过程中遇到的MissingSchemaError问题。
我就不多说了,直接上代码吧!
这是user文件:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var userSchema = new Schema({
uid: {
type: Number,
unique: true
},
name: {
type: String,
required: true
},
phone: {
type: String,
required: true
},
password: {
type: String,
required: true
}
}
);
module.exports = mongoose.model('User', userSchema);
这是user的controller文件:
var mongoose = require('mongoose'),
User = mongoose.model('User'),
_ = require('lodash');
/*增加一个user数据*/
exports.create = function (req, res, next) {
var data = req.body;
console.log(req + '%%%%%%%%%req content');
console.log(data + '@@@@@@@@data');
User.save(data, function (err) {
console.log(err);
}, function (res) {
console.log(res);
});
return next()
};
这是router文件:
var user = require('./controllers/user');
module.exports = function (app) {
app.post('api/user/:id',user.create());
};
这是server文件:
var express = require('express'),
//session = require('express-session'),
mongoose = require('mongoose'),
router = require('./lib/router'),
app = express();
app.use(express.static(__dirname + '/'));
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
router(app);
console.log('you have logged in successfully!');
app.listen(4000);
然后是报错的代码:
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
at Mongoose.model (e:\自己做的项目\TradingPlatform\node_modules\mongoose\lib\index.js:350:13)
at Object.<anonymous> (e:\自己做的项目\TradingPlatform\lib\controllers\user.js:9:21)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (e:\自己做的项目\TradingPlatform\lib\router.js:7:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (e:\自己做的项目\TradingPlatform\server.js:10:14)
Process finished with exit code 1
求各位大神解答
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
controllers/user
贴下这个文件的代码吧