2015-05-28 58 views
2

在node.js中絕對的初學者我正在做一個路線文件,該文件是低於Node.js的路線錯誤

productCategoryRouteConfig.js

function productCategoryRouteConfig(app){ 
    this.app = app; 
    this.routesTable = []; 
    this.init(); 
} 

productCategoryRouteConfig.prototype.init = function(){ 
    this.addRoutes(); 
    this.processRoutes(); 
} 

productCategoryRouteConfig.prototype.processRoutes = function(){ 

    this.routesTable.forEach(function(route){ 
     if(route.requestType === 'get') 
     { 
      this.app.get(route.requestUrl, route.callbackFunction) 
     } 
    }); 
} 

productCategoryRouteConfig.prototype.addRoutes = function(){ 

    this.routesTable.push({ 
     requestType: 'get', 
     requestUrl: '/createProductCategory', 
     callbackFunction: function(request, response){ 
      response.render('createProductCategory', {title: "Create Product Category"}); 
     } 
    }); 
} 

module.exports = productCategoryRouteConfig; 

app.js文件低於

var express = require('express'); 
var path = require('path'); 
var favicon = require('serve-favicon'); 
var logger = require('morgan'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 

var routes = require('./routes/index'); 
var users = require('./routes/users'); 
var productCategoryRoute = require('./routes/productCategoryRouteConfig'); 

var app = express(); 


// view engine setup 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'ejs'); 

app.use('/bower_components', express.static(__dirname + '/bower_components')); 

// uncomment after placing your favicon in /public 
//app.use(favicon(__dirname + '/public/favicon.ico')); 
app.use(logger('dev')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 

app.use('/', routes); 
app.use('/users', users); 



/******BEGIN CUSTOM ROUTES*********/ 
new productCategoryRoute(app) 
/******END CUSTOM ROUTES*********/ 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// error handlers 

// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
     message: err.message, 
     error: err 
    }); 
    }); 
} 



// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
    message: err.message, 
    error: {} 
    }); 
}); 

但使用此命令DEBUG=nodecrud:* ./bin/www運行NPM服務器時,我得到了以下錯誤

/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:22 
      this.app.get(route.requestUrl, route.callbackFunction) 
        ^
TypeError: Cannot read property 'get' of undefined 
    at /home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:22:21 
    at Array.forEach (native) 
    at productCategoryRouteConfig.processRoutes (/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:18:22) 
    at productCategoryRouteConfig.init (/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:13:10) 
    at new productCategoryRouteConfig (/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:8:10) 
    at Object.<anonymous> (/home/sharif/Sites/node/angularmysqlnode/nodecrud/app.js:39:1) 
    at Module._compile (module.js:460:26) 
    at Object.Module._extensions..js (module.js:478:10) 
    at Module.load (module.js:355:32) 
    at Function.Module._load (module.js:310:12) 

我不知道爲什麼這個錯誤就要到了,你能不能幫我解決這個錯誤,請

什麼想法?

+0

這個問題通過此[鏈接]已解決(http://stackoverflow.com/questions/15604848/express-js-this-undefined-after-routing-with-app-get)! –

回答

1

你是不是在傳遞這個app LINE-

var productCategoryRoute = require('./routes/productCategoryRouteConfig'); 

做到這一點

var productCategoryRoute = require('./routes/productCategoryRouteConfig')(app); 

,並把它之後

var app = express(); 
+0

謝謝@戴夫樁,我這樣做,現在這個錯誤就要到了'/home/sharif/Sites/node/angularmysqlnode/nodecrud/routes/productCategoryRouteConfig.js:8 this.init(); ^ 類型錯誤:未定義不是function' – Amjad

+0

一般而言,我不知道,編輯問題刪除原來的錯誤信息,並把在新無關一個是一個好主意,因爲它那種使得答案上面沒有任何意義。 –

+0

是嗎? @戴夫樁 – Amjad