今天早上我醒来发现 Nodejitsu 出现以下错误:
Warning: connection.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process.
'好吧!' 我心想——然后我去寻找更合适的东西。我选择了 Redis,并立即开始实施connect-redis
并node-redis
进入我的应用程序。
但是,在反复尝试修复后,以下错误继续出现。错误如下:
TypeError: Cannot set property 'loggedIn' of undefined
at /Users/Ryan/Aggregus/server.js:300:25
at Promise.<anonymous> (/Users/Ryan/Aggregus/object_models/user.js:60:5)
at Promise.<anonymous> (/Users/Ryan/Aggregus/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/Users/Ryan/Aggregus/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (/Users/Ryan/Aggregus/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
at /Users/Ryan/Aggregus/node_modules/mongoose/lib/query.js:1813:13
at model.Document.init (/Users/Ryan/Aggregus/node_modules/mongoose/lib/document.js:243:11)
at completeOne (/Users/Ryan/Aggregus/node_modules/mongoose/lib/query.js:1811:10)
at /Users/Ryan/Aggregus/node_modules/mongoose/lib/query.js:1779:11
loggedIn
只是指req.session.loggedIn
,我用来跟踪登录用户的会话变量。以下是有问题的应用程序代码:
var express = require('express');
var crypto = require('crypto');
var mongoose = require('mongoose');
var nodemailer = require('nodemailer');
var request = require('request');
var underscore = require('underscore');
var mandrill = require('node-mandrill')('0bFNPDenlDiZtu7aXujDQQ');
var connect = require('connect');
var engines = require('consolidate');
var fs = require('fs');
var redis = require("redis").createClient();
var RedisStore = require('connect-redis')(express);
var sessionStore = new RedisStore({
client: redis
});
var stripe_api_key = 'KEY';
var Stripe = require('stripe')(stripe_api_key);
var app = express();
app.configure(function() {
app.engine('html', engines.jade);
app.set('view engine', 'html');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({secret: "svtabyrki4q786as37c785ta8vi56aiw4i8w467acv", store: sessionStore}));
app.use(express.static(__dirname + '/www'));
app.use(express.compress());
app.use(app.router);
});
// Mongoose Connection
var db = mongoose.connection;
var dbURI = 'HIDDENURL';
db.on('disconnected', function() {
mongoose.connect(dbURI, {server:{auto_reconnect:true}});
});
db.on('error', function(error) {
console.error('Error in MongoDb connection: ' + error);
mongoose.disconnect();
});
mongoose.connect(dbURI, {server:{auto_reconnect:true}});
// Mongoose Object Models
var User = require('./object_models/user')(mongoose, nodemailer, mandrill);
var Notifications = require('./object_models/notification')(mongoose, nodemailer);
var Experience = require('./object_models/experience')(mongoose, nodemailer);
var Booking = require('./object_models/booking')(mongoose, nodemailer);
var Review = require('./object_models/review')(mongoose, nodemailer);
var Heart = require('./object_models/heart')(mongoose, nodemailer);
var Message = require('./object_models/message')(mongoose, nodemailer);
// Nodemailer Settings
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "ryan@aggregus.com",
pass: pwd
}
});
// Bootstrap Call
app.get('/', function(req, res) {
res.render('../index.html');
console.log("WUSUP");
});
为了提供进一步的清晰度,这是我尝试过的,直到这一点无济于事:
- 正如该主题的大多数文章所建议的那样,我已
app.use(express.cookieParser());
在电话会议之前放置。express.session
- 我确保我的
new RedisStore
调用client
在初始化时直接引用。 - 我已经
app.use(app.router)
在两个初始化调用express.session
和'express.cookieParser'之后放置了。 - 通过调用,我的 Redis 服务器功能齐全
createClient()
。
通过防止在 Express 的 sessionStore 被初始化之前调用一个相当微不足道的初始化来解决这个类似的问题。app.use
app.router
因此,TL;DR:在使用中connect-redis
,我的会话变量返回undefined
。我已经在网上搜索了答案,我唯一的线索app.router
是在 Express 自己的会话处理之前开始。发生那个小故障的地方超出了我的范围。Stack Overflow 之神怜悯我可怜的灵魂。
编辑:认为 package.json 可能便于参考。通过 NodeJits 生成:
{
"name": "Aggregus",
"subdomain": "aggregus-Aggregus",
"scripts": {
"start": "node server.js"
},
"version": "0.0.0-14",
"engines": {
"node": "0.8.x"
},
"dependencies": {
"connect": "2.7.8",
"consolidate": "0.9.1",
"express": "3.2.2",
"jade": "0.30.0",
"mongoose": "3.6.9",
"node-mandrill":"1.0.1",
"nodemailer": "0.4.1",
"request":"2.22.0",
"stripe": "1.3.0",
"underscore":"1.4.4"
}
}