以前我使用快速会话它工作得很好,现在我已经改为cloudant会话,当我尝试运行这个并注册/登录它说req.session没有定义哪个适用于正常快速会话有人能告诉我我做了什么错误或如何触发或创建一个会话 这是我的app.js文件
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var CloudantStore = require('connect-cloudant-store')(session);
var MongoStore = require('connect-mongo')(session);
//connect to MongoDB
mongoose.connect('cloudant url');
//var db = mongoose.connection;
var Cloudant = require('cloudant');
var me = "username"; // Set this to your own account
var password = "password";
// Initialize the library with my account.
var cloudant = Cloudant({
account: me,
password: password
});
var db = cloudant.db.use('placements-test')
cloudant.db.list(function (err, allDbs) {
console.log('Available Databases: %s', allDbs.join(', '))
});
var store = new CloudantStore(
{
url: 'url',
databaseName: "placement-test"
}
);
store.on('connect', function() {
// Cloudant Session store is ready for use
setInterval(function() { store.cleanupExpired(); }, 3600 * 1000);
});
store.on('disconnect', function() {
// failed to connect to cloudant db - by default falls back to MemoryStore
});
store.on('error', function(err) {
// You can log the store errors to your app log
});
app.use(session({
secret: 'keyboardcat',
store:store,
saveUninitialized: false,
resave: true
}));
// parse incoming requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// serve static files from template
app.use(express.static(__dirname + '/templateLogReg'));
// include routes
var routes = require('./routes/router');
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});
// listen on port 3000
app.listen(8080, function () {
console.log('Express app listening on port 8080');
});

这是我的路由文件
var express = require('express');
var router = express.Router();
var User = require('../models/user');
var path = require('path');
var Cloudant = require('cloudant');
var me = "username"; // Set this to your own account
var password = "password";
var cloudant = Cloudant({
account: me,
password: password
});
var db = cloudant.db.use('placements-test')
// GET route for reading data
router.get('/', function (req, res, next) {
return res.sendFile(path.join(__dirname + '/templateLogReg/index.html'));
});
//POST route for updating data
router.post('/', function (req, res, next) {
// confirm that user typed same password twice
if (req.body.password !== req.body.passwordConf) {
var err = new Error('Passwords do not match.');
err.status = 400;
res.send("passwords dont match");
return next(err);
}
if (req.body.email &&
req.body.username &&
req.body.password &&
req.body.passwordConf) {
var userData = {
email: req.body.email,
username: req.body.username,
password: req.body.password,
passwordConf: req.body.passwordConf,
}
db.insert(userData, function (err, user) {
if (err) {
return console.log('[db.insert] ', err.message);
}
else {
console.log('You have created a New User');
console.log(user);
console.log(req.session);
req.session.user = user._id;
return res.redirect('/profile');
}
});
/*
User.create(userData, function (error, user) {
if (error) {
return next(error);
} else {
req.session.userId = user._id;
return res.redirect('/profile');
}
});*/
} else if (req.body.logemail && req.body.logpassword) {
User.authenticate(req.body.logemail, req.body.logpassword, function (error, user) {
if (error || !user) {
var err = new Error('Wrong email or password.');
err.status = 401;
return next(err);
} else {
req.session.userId = user._id;
return res.redirect('/profile');
}
});
} else {
var err = new Error('All fields required.');
err.status = 400;
return next(err);
}
})
// GET route after registering
router.get('/profile', function (req, res, next) {
User.findById(req.session.userId)
.exec(function (error, user) {
if (error) {
return next(error);
} else {
if (user === null) {
var err = new Error('Not authorized! Go back!');
err.status = 400;
return res.sendFile(path.resolve('templateLogReg/index.html'));;
} else {
return res.sendFile(path.resolve('templateLogReg/dashboard.html'));
}
}
});
});
// GET for logout logout
router.get('/logout', function (req, res, next) {
if (req.session) {
// delete session object
req.session.destroy(function (err) {
if (err) {
return next(err);
} else {
return res.redirect('/');
}
});
}
});
module.exports = router;

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
//connect to MongoDB
mongoose.connect('mongodb://localhost/testForAuth');
var db = mongoose.connection;
//handle mongo error
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
});
//use sessions for tracking logins
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
// parse incoming requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// serve static files from template
app.use(express.static(__dirname + '/templateLogReg'));
// include routes
var routes = require('./routes/router');
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});
// listen on port 3000
app.listen(3000, function () {
console.log('Express app listening on port 3000');
});

这是router.js
var express = require('express');
var router = express.Router();
var User = require('../models/user');
var path = require('path');
// GET route for reading data
router.get('/', function (req, res, next) {
return res.sendFile(path.join(__dirname + '/templateLogReg/index.html'));
});
//POST route for updating data
router.post('/', function (req, res, next) {
// confirm that user typed same password twice
if (req.body.password !== req.body.passwordConf) {
var err = new Error('Passwords do not match.');
err.status = 400;
res.send("passwords dont match");
return next(err);
}
if (req.body.email &&
req.body.username &&
req.body.password &&
req.body.passwordConf) {
var userData = {
email: req.body.email,
username: req.body.username,
password: req.body.password,
passwordConf: req.body.passwordConf,
}
User.create(userData, function (error, user) {
if (error) {
return next(error);
} else {
console.log(req.session);
console.log(req.session.userId);
req.session.userId = user._id;
return res.redirect('/profile');
}
});
} else if (req.body.logemail && req.body.logpassword) {
User.authenticate(req.body.logemail, req.body.logpassword, function (error, user) {
if (error || !user) {
var err = new Error('Wrong email or password.');
err.status = 401;
return next(err);
} else {
console.log(req.session);
console.log(req.session.userId);
req.session.userId = user._id;
return res.redirect('/profile');
}
});
} else {
var err = new Error('All fields required.');
err.status = 400;
return next(err);
}
})
// GET route after registering
router.get('/profile', function (req, res, next) {
User.findById(req.session.userId)
.exec(function (error, user) {
if (error) {
return next(error);
} else {
if (user === null) {
var err = new Error('Not authorized! Go back!');
err.status = 400;
return res.sendFile( path.resolve('templateLogReg/index.html') );;
} else {
return res.sendFile( path.resolve('templateLogReg/dashboard.html') );
}
}
});
});
// GET for logout logout
router.get('/logout', function (req, res, next) {
if (req.session) {
// delete session object
req.session.destroy(function (err) {
if (err) {
return next(err);
} else {
return res.redirect('/');
}
});
}
});
module.exports = router;

请帮忙
答案 0 :(得分:0)
启用connect-cloudant-store模块的调试后会得到什么?
fragmentManager = getChildFragmentManager();
另外我会记录商店的错误:
export DEBUG=connect:cloudant-store;npm start
此问题的一个可能来源可能是会话数据库未在cloudant中创建。如上所述启用debuging可以帮助您捕获这样的错误。