1
我是Node和MongoDB的新手。我使用貓鼬在Mongo上創建模式。我已經在1個models.js文件中創建了兩個模式,如下所示1 of 2 Mongoose模式未初始化
var mongoose = require('mongoose');
var postSchema = new mongoose.Schema({
username: String,
text: String,
created_at: {type: Date, default: Date.now}
});
var userSchema = new mongoose.Schema({
username: String,
password: String,
created_at: {type: Date, default: Date.now}
});
//declaring a model which has schema userSchema
mongoose.model("User", userSchema);
mongoose.model("Post", postSchema);
問題是我的用戶架構正在初始化並且工作正常。但帖子模式是一個問題。 這是我得到啓動服務器時出現錯誤:
C:\Users\rohit\Desktop\projects\chirp\module4\start\node_modules\mo
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Post".
下面是從實際調用後的架構,使數據庫api.js我的片段查詢
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Post = mongoose.model('Post');
...
router.route('/posts')
.get (function(req, res){
Post.find(function(err, data){
if(err){
return res.send(500, err)
}
return res.send(data)
})
})
這裏是代碼從使用用戶模式我auth.js文件片段,它工作正常
var LocalStrategy = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');
var mongoose = require('mongoose');
var User = mongoose.model('User');
var Post = mongoose.model('Post');
module.exports = function(passport){
// Passport needs to be able to serialize and deserialize users to support persistent login sessions
passport.serializeUser(function(user, done) {
console.log('serializing user:',user._id);
return done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user){
if (err){
return done(err, false)
}
if(!user){
return done('User not found', false)
}
return done(user, true);
})
});
但後來爲什麼會爲用戶模式和,也不適用於郵政架構工作? – rsisaudia
這很奇怪。看看http://stackoverflow.com/questions/20832126/missingschemaerror-schema-hasnt-been-registered-for-model-user – deChristo