2013-12-18 16 views
1

我試圖將我的Express應用程序的會話存儲在MongoStore中,但在連接時出現錯誤。該錯誤是一個巨大的對象/ JSON,我不能做的意義,所以我想盡替代我在網上找到,但到目前爲止沒有運氣...在Express中使用MongoStore時出錯

這裏是應用程序配置:

var express = require('express'), 
    MongoStore = require('connect-mongo')(express), 
    passport = require('passport'); 

var app = express(); 

app.configure(function(){ 
    app.use(express.compress()); 
    app.use(express.static(path.join(__dirname, 'public'))); 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'ejs'); 
    app.set('port', process.env.PORT || 8000); 
    app.use(express.favicon(__dirname + '/public/img/favicon.ico')); 
    app.use(express.logger('dev')); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(express.cookieParser('secret')); 
    app.use(express.session({ 
      secret: 'secret', 
      store: new MongoStore({ 
        db: dbOptions.db, 
        host: dbOptions.host, 
        port: dbOptions.port, 
        username: dbOptions.username, 
        password: dbOptions.password 
      }, 
      function(err){ 
        console.log(err || 'connect-mongodb setup ok'); 
      }) 
    })); 
    app.use(passport.initialize()); 
    app.use(passport.session()); 
    app.use(app.router); 
}); 

app.configure('development', function(){ 
    app.use(express.errorHandler()); 
}); 

這是被記錄在控制檯上的錯誤:

{ db: 
    { domain: null, 
    _events: {}, 
    _maxListeners: 10, 
    databaseName: 'dbname', 
    serverConfig: 
     { domain: null, 
     _events: {}, 
     _maxListeners: 10, 
     _callBackStore: [Object], 
     _commandsStore: [Object], 
     auth: [Object], 
     _dbStore: [Object], 
     host: 'ec2-xx-xxx-xxx-xx.eu-west-1.compute.amazonaws.com', 
     port: 27017, 
     options: [Object], 
     internalMaster: true, 
     connected: true, 
     poolSize: 5, 
     disableDriverBSONSizeCheck: false, 
     _used: true, 
     replicasetInstance: null, 
     emitOpen: false, 
     ssl: false, 
     sslValidate: false, 
     sslCA: null, 
     sslCert: undefined, 
     sslKey: undefined, 
     sslPass: undefined, 
     serverCapabilities: [Object], 
     name: 'ec2-xx-xx-xxx-xx.eu-west-1.compute.amazonaws.com:27017', 
     _readPreference: null, 
     socketOptions: [Object], 
     logger: [Object], 
     eventHandlers: [Object], 
     _serverState: 'connected', 
     _state: [Object], 
     recordQueryStats: false, 
     socketTimeoutMS: [Getter/Setter], 
     db: [Circular], 
     dbInstances: [Object], 
     connectionPool: [Object], 
     isMasterDoc: [Object] }, 
    options: { w: 1 }, 
    _applicationClosed: false, 
    slaveOk: false, 
    bufferMaxEntries: -1, 
    native_parser: undefined, 
    bsonLib: 
     { Code: [Function: Code], 
     Symbol: [Function: Symbol], 
     BSON: [Object], 
     DBRef: [Function: DBRef], 
     Binary: [Object], 
     ObjectID: [Object], 
     Long: [Object], 
     Timestamp: [Object], 
     Double: [Function: Double], 
     MinKey: [Function: MinKey], 
     MaxKey: [Function: MaxKey], 
     promoteLongs: true }, 
    bson: { promoteLongs: true }, 
    bson_deserializer: 
     { Code: [Function: Code], 
     Symbol: [Function: Symbol], 
     BSON: [Object], 
     DBRef: [Function: DBRef], 
     Binary: [Object], 
     ObjectID: [Object], 
     Long: [Object], 
     Timestamp: [Object], 
     Double: [Function: Double], 
     MinKey: [Function: MinKey], 
     MaxKey: [Function: MaxKey], 
     promoteLongs: true }, 
    bson_serializer: 
     { Code: [Function: Code], 
     Symbol: [Function: Symbol], 
     BSON: [Object], 
     DBRef: [Function: DBRef], 
     Binary: [Object], 
     ObjectID: [Object], 
     Long: [Object], 
     Timestamp: [Object], 
     Double: [Function: Double], 
     MinKey: [Function: MinKey], 
     MaxKey: [Function: MaxKey], 
     promoteLongs: true }, 
    _state: 'connected', 
    pkFactory: 
     { [Function: ObjectID] 
     index: 16051204, 
     createPk: [Function: createPk], 
     createFromTime: [Function: createFromTime], 
     createFromHexString: [Function: createFromHexString] }, 
    forceServerObjectId: false, 
    safe: false, 
    notReplied: {}, 
    isInitializing: true, 
    openCalled: true, 
    commands: [], 
    logger: { error: [Function], log: [Function], debug: [Function] }, 
    tag: 1387402758191, 
    eventHandlers: 
     { error: [], 
     parseError: [], 
     poolReady: [], 
     message: [], 
     close: [] }, 
    serializeFunctions: false, 
    raw: false, 
    recordQueryStats: false, 
    retryMiliSeconds: 1000, 
    numberOfRetries: 60, 
    readPreference: undefined }, 
    collectionName: 'sessions', 
    internalHint: null, 
    opts: {}, 
    slaveOk: false, 
    serializeFunctions: false, 
    raw: false, 
    readPreference: 'primary', 
    pkFactory: 
    { [Function: ObjectID] 
    index: 16051204, 
    createPk: [Function: createPk], 
    createFromTime: [Function: createFromTime], 
    createFromHexString: [Function: createFromHexString] }, 
    serverCapabilities: undefined } 

這是非常模糊的,所以我不知道在哪個方向,我應該尋找。

編輯1:

好,謝謝,現在更有意義,但事情是我想記錄一個錯誤,因爲該會議不存儲在MongoDB中,我不明白爲什麼。我加了周圍的MongoStore的設置一個try catch塊,看是否被觸發一個錯誤,是沒有的,所以我不知道爲什麼它不工作...

這是怎麼passportjs存儲和檢索會話:

passport.serializeUser(function(user, done) { 
    done(null, user._id); 
}); 

passport.deserializeUser(function(_id, done) { 
    var userProvider = new UserProvider(); 
    userProvider.findUser({ _id: _id }, function (err, users) { 
      if(users.length > 0) { done(null, users[0]); } 
      else { done(err, null); } 
    }); 
}); 

我錯過了什麼?

感謝您的幫助!

回答

1

你的問題出在下面一行:

store: new MongoStore({ 
    db: dbOptions.db, 
    host: dbOptions.host, 
    port: dbOptions.port, 
    username: dbOptions.username, 
    password: dbOptions.password 
}, 
function(err){ 
    console.log(err || 'connect-mongodb setup ok'); 
}) 

只需刪除最後一個功能,我相信它應該工作,你最初的預期

相反,安德烈的回答那裏,MongoStore構造函數接受回電話。然而,這不是出於錯誤處理的目的 - 它實際上傳回了MongoStore的集合屬性(這就是爲什麼console.log發出它所做的)。如果出現錯誤,MongoStore會簡單地拋出一個錯誤。你可以在源代碼by clicking here中找到它。該回調在164行調用。

+0

MongoStore確實有回調。構造函數的參數爲​​2,第二個參數確實是一個回調函數。但是,與節點標準相反,回調中的第一個參數不是錯誤,而是MongoStore的collection屬性。 – srquinn

+0

你是對的,這是商店的回調,而不是會話。 –

+1

您錯過了回撥的路線......請遵循代碼更進一步。電話是在線164 https://github.com/kcbanner/connect-mongo/blob/master/lib/connect-mongo.js#L164 – srquinn

相關問題