Nodejs和Expressjs - 未定义变量

时间:2014-03-15 15:28:39

标签: javascript node.js express mongoose

我有以下代码,但我收到错误消息未定义成员。所以我尝试将/路由移动到db.once块但是它只是停止而没有做任何事情。

作为这个领域的初学者,我被卡住了所以任何帮助都会非常感激!

var express = require( 'express' );
var mongoose = require('mongoose');
var http    = require( 'http' );
var path    = require( 'path' );
var app     = express();

// all environments
app.set( 'port', process.env.PORT || 3001 );
app.use( express.logger( 'dev' ));
app.use( express.bodyParser());
app.use( express.json());
app.use( express.urlencoded());
app.use( express.methodOverride());
app.use( app.router );

// development only
if ( 'development' == app.get( 'env' )) {
  app.use( express.errorHandler());
}

mongoose.connect('mongodb://localhost/dbname');
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  var memberSchema = new mongoose.Schema({
    name: String,
    email: String
  });
  var Member = mongoose.model( 'Member', memberSchema );

});


//******ROUTES*******//
app.get('/', function(req, res){
  Member.find(function(err, members) {
    if (err) return console.error(err);
    console.dir(members);
  });
});

http.createServer( app ).listen( app.get( 'port' ), function(){
  console.log( 'Express server listening on port ' + app.get( 'port' ));
} );

0 个答案:

没有答案