2016-11-22 50 views
1

我需要檢索sessionStore(MongoStore)列表,會話和刪除以前的登錄會話。我正在使用快速會話將會話存儲在數據庫中。從sessionStore檢索會話列表並銷燬用戶以前各屆會議的NodeJS

var userId = req.query.userid; 
    if (!userId) 
    return res.status(400).send({ 
     message: errorHandler.getErrorMessage('No user found in input request') 
    }); 

    var store = req.sessionStore; 
    var sessionsColl = store.db.collection('sessions'); 

    sessionsColl.find({ 
    'session.passport.user': userId, 
    '_id': { '$ne': req.sessionID } 
    }, function (err, userSessions) { 
    console.log(userSessions); 
    if (userSessions !== null && typeof userSessions._id !== 'undefined') { 
     store.destroy(userSessions._id, function (err, dat) { 
     console.log(dat); 
     });  
    } 
    }); 

userSessions對象我得到一個對象,而不是收集和我不明白我怎麼可以從userSessions對象

userSessions object response

回答

1

最後獲取會話列表一點我就能解決問題 這裏是我的代碼

exports.logoutFromPreviousDevices = function (req, res) { 
    var userId = req.query.userid; 
    if (!userId) 
    return res.status(400).send({ 
     message: errorHandler.getErrorMessage('No user found in input request') 
    }); 

    var store = req.sessionStore; 
    var sessionsColl = store.db.collection('sessions'); 

    sessionsColl.find({ 
    // 'session.passport.user': userId, 
    // we are tryin to remove all sessions, you can leave current 
    // '_id': { '$ne': req.sessionID } 
    }, function (err, userSessions) { 
    if (userSessions !== null) { 
     userSessions.toArray(function (a, sessionsData) { 
     sessionsData.forEach(function (element, index) { 
      var data = JSON.parse(element.session); 
      if (element._id !== req.sessionID && req.query.userid === data.passport.user) { 
      store.destroy(element._id, function (destroyerr, dat) { 
       if (destroyerr) 
       return res.status(400).send({ 
        message: errorHandler.getErrorMessage(destroyerr) 
       }); 
       res.jsonp({ status: 'Previous session deleted' }); 
      }); 
      } 
     }); 
     }); 
    } else { 
     res.jsonp({ status: 'No session found' }); 
    } 
    }); 
};