TypeError: Undefined type at `paths.sid`
发布于 10个月前 作者 imingyu 724 次浏览

#关于标题 *这是一个报错信息; *别人发生类似错误时可快速定位,找到答案;

#我遇到什么问题? 我在使用Express + mongoose + session-mongoose 开发网站应用时,发生了错误,我的代码如下:

//数据库
var dbconfig = require('./dbconfig.js');//读取配置
var mongoose = require("mongoose");
mongoose.connect(dbconfig.mongoose_constr);//创建连接

/*session支持*/
app.use(express.cookieParser());
var SessionStore = require("session-mongoose")(express);
var _sstore = new SessionStore({
    connection : mongoose.connection,
    interval : 120000,
    sweeper: false,                     //Session过期后,不清除数据
    ttl: 300,                           //Sessuib过期时间,单位:秒
    modelname: 'SessionUser'            //mongoose model name
});
app.use(express.session({
    store : _sstore,
    cookie: { maxAge: 900000 }
}));

我在运行网站时,控制台报错如下:

E:\project>node app
E:\project\node_modules\mongoose\li
b\schema.js:362
    throw new TypeError('Undefined type at `' + path +
          ^
TypeError: Undefined type at `paths.sid`
  Did you try nesting Schemas? You can only nest using refs or arrays.
    at Function.Schema.interpretAsType (E:\project\node_modules\mongoose\lib\schema.js:362:11)
    at Schema.path (E:\project\node
_modules\mongoose\lib\schema.js:305:29)
    at Schema.add (E:\project\node_
modules\mongoose\lib\schema.js:217:12)
    at Schema.add (E:\project\node_
modules\mongoose\lib\schema.js:212:14)
    at new Schema (E:\project\node_
modules\mongoose\lib\schema.js:73:10)
    at NativeConnection.Connection.model (E:\project\node_modules\mongoose\lib\connection.js:584:14)
    at new SessionStore (E:\project
\node_modules\session-mongoose\index.js:95:35)
    at Object.<anonymous> (E:\project\app.js:38:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

谁能告诉我这个错误改怎么解决?

另外,怎样让mongoose的数据库连接只配置一次,就可以全局通用?我是这样做的,你们还有其他更好的方法吗?求分享~~

/*ProjectSchema.js*/
module.exports = function(mongoose){
//mongoose:从顶层传入的mongoose实例对象
    var Schema = mongoose.Schema;
    var ProjectSchema= {
        UserSchema : new Schema({
            UserName : {type: String, index: true},
            PassWord : String,
            IsDeleted : {type: Boolean, default: false},
            CreateDate : { type: Date, default: Date.now }
        },{
            _id: true,
            collection: 'core.User'
        }),
        SessionUserSchema : new Schema({
            UserId : Schema.Types.ObjectId,
            UserName : {type: String, index: true},
            LoginTime : { type: Date, default: Date.now }
        },{
            _id: true,
            collection: 'safe.SessionUser'
        })
    };
    return ProjectSchema;
};

/*ProjectModel.js*/
module.exports = function(mongoose){
//mongoose:从顶层传入的mongoose实例对象
    var prSchema = require('./ProjectSchema.js')(mongoose);

    var ProjectModel= {
        UserModel : mongoose.model('User',prSchema .UserSchema),
        SessionUser : mongoose.model('SessionUser',prSchema .SessionUserSchema)
    };

    return ProjectModel;
}

回到顶部