我正在使用 express 和 mongoose-auth 编写一个 node.js 应用程序。当我运行我的应用程序时,我得到一个包含以下堆栈跟踪的网页。
TypeError: Cannot read property 'auth' of undefined
at HTTPServer. (/home/levi/Projects/Cloudrr/node_modules/mongoose-auth/node_modules/everyauth/lib/expressHelper.js:7:27)
at ServerResponse._render (/home/levi/Projects/Cloudrr/node_modules/express/lib/view.js:393:58)
at ServerResponse.render (/home/levi/Projects/Cloudrr/node_modules/express/lib/view.js:315:17)
at /home/levi/Projects/Cloudrr/app.js:41:7
at callbacks (/home/levi/Projects/Cloudrr/node_modules/express/lib/router/index.js:272:11)
at param (/home/levi/Projects/Cloudrr/node_modules/express/lib/router/index.js:246:11)
at pass (/home/levi/Projects/Cloudrr/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/home/levi/Projects/Cloudrr/node_modules/express/lib/router/index.js:280:4)
at Object.handle (/home/levi/Projects/Cloudrr/node_modules/express/lib/router/index.js:45:10)
at next (/home/levi/Projects/Cloudrr/node_modules/express/node_modules/connect/lib/http.js:201:15)
这是我的 app.js:
/**
* Module dependencies.
*/
var express = require('express');
var mongoose = require('mongoose');
var mongooseAuth = require('mongoose-auth');
var model = require("./models");
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(mongooseAuth.middleware())
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Database
var Proffile = mongoose.model('Profile', model.Profile);
var Cloudrr = mongoose.model('Cloudrr', model.Cloudrr);
var Cloud = mongoose.model('Cloud', model.Cloud);
var User = mongoose.model('User', model.UserSchema);
mongoose.connect('mongodb://localhost/cloudrr');
// Routes
app.get('/', function(req, res){
res.render('index', {
title: 'Cloudrr | Lorem ipsum'
});
});
app.get('/index', function(req, res){
res.render('index', {
title: 'Cloudrr | Lorem ipsum'
});
});
app.get('/left', function(req, res){
if(req.loggedIn == True)
{
Profile.findById(req.user, function(user){
res.render('left', {
title: "Welcome back!",
user_id: user.profileID,
avatar: user.avatar,
followers: user.followers.length(),
following: user.following.length(),
});
});
}
else
{
res.render("left", {
title: "you need to log in."
});
}
});
mongooseAuth.helpExpress(app);
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
和 package.json。
{
"name": "cloudrr"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.4.6"
, "jade": ">= 0.0.1"
, "mongoose": ">= 2.1.3"
, "mongoose-types": ">= 1.0.3"
, "mongoose-auth": ">= 0.0.11"
}
}
我究竟做错了什么?堆栈跟踪指向第 41 行,它位于第一条路由的中间。谷歌搜索在everyauth 问题跟踪器中向我显示了几个具有相同错误的问题,但没有提供的解决方案有效。感谢您的时间。