TypeError:无法读取属性'会话'的未定义

TypeError: Cannot read property 'session' of undefined

本文关键字:未定义 会话 属性 读取 TypeError      更新时间:2023-09-26

我正在使用angular-fullstack生成器,到目前为止一切都运行顺利。我不知道我更改了什么,但现在我的服务器任务因此错误而暂停。

/Users/User/APP/server/config/express.js:28
secret: _environment2.default.secrets.session,
                                     ^
TypeError: Cannot read property 'session' of undefined
at Object.exports.default (express.js:41:13)
at Object.<anonymous> (app.js:23:1)
at Module._compile (module.js:541:32)
at loader (/Users/User/APP/node_modules/babel-register/lib/node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/User/APP/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/User/APP/server/index.js:12:28)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)

这是我的express.js到app.use(session({}):

/**
* Express configuration
*/
'use strict';
import express from 'express';
import favicon from 'serve-favicon';
import morgan from 'morgan';
import compression from 'compression';
import bodyParser from 'body-parser';
import methodOverride from 'method-override';
import cookieParser from 'cookie-parser';
import errorHandler from 'errorhandler';
import path from 'path';
import lusca from 'lusca';
import config from './environment';
import passport from 'passport';
import session from 'express-session';
import sqldb from '../sqldb';
import expressSequelizeSession from 'express-sequelize-session';
var Store = expressSequelizeSession(session.Store);
export default function(app) {
var env = app.get('env');
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
// Persist sessions with MongoStore / sequelizeStore
// We need to enable sessions for passport-twitter because it's an
// oauth 1.0 strategy, and Lusca depends on sessions
app.use(session({
secret: config.secrets.session,
saveUninitialized: true,
resave: false,
store: new Store(sqldb.sequelize)
}));

这是我的配置/环境的index.js:

'use strict';
var path = require('path');
var _ = require('lodash');
function requiredProcessEnv(name) {
if (!process.env[name]) {
throw new Error('You must set the ' + name + ' environment variable');
}
return process.env[name];
}
// All configurations will extend these options
// ============================================
var all = {
env: process.env.NODE_ENV,
// Root path of server
root: path.normalize(__dirname + '/../../..'),
// Server port
port: process.env.PORT || 9000,
// Server IP
ip: process.env.IP || '0.0.0.0',
// Should we populate the DB with sample data?
seedDB: false,
// Secret for session, you will want to change this and make it an  environment variable
secrets: {
session: process.env.session || "wav"
},
// Export the config object based on the NODE_ENV
// ==============================================
module:exports = _.merge(
all,
require('./shared'),
require('./' + process.env.NODE_ENV + '.js') || {})};

更新:添加
在index.js的最后一行的var config = _.merge(...); console.log(config); module.exports = config;,现在在语法上考虑该错误。错误:
line 39 col 7 Expected ':' and instead saw '.'. line 42 col 3 Unexpected 'var'. line 42 col 3 Expected an identifier and instead saw 'var'. line 42 col 7 Expected ')' and instead saw 'config'. line 42 col 14 Bad assignment. line 42 col 27 Expected an identifier and instead saw ')'. line 42 col 27 Expected an identifier and instead saw ')'. line 42 col 28 Expected ')' and instead saw '; line 42 col 30 Expected '}' to match '{' from line 15 and instead saw 'console'. line 42 col 42 'config' is not defined. line 42 col 68 'config' is not defined.

这里找到并讨论了解决方案:Index.js中的语法错误。类型错误是由潜在的语法错误引起的。