我正在使用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条线,它位于第一条线路的中间。谷歌搜索向我展示了每个问题跟踪器中的一些问题,但是相同的错误,但没有提供任何解决方案。谢谢你的时间。
答案 0 :(得分:0)
我相信您错过了必需的步骤,请参阅mongoose-auth documentation中的架构装饰。
你几周前可能就在我身边。如果您当前的应用程序是新的,因此仍然很小,您可能希望使用mongoose-auth示例程序启动一个新项目。确认可以登录和注销后,可以添加应用程序功能。
答案 1 :(得分:0)
事实证明问题是不平衡的括号,因此修复支架平衡可以解决问题。