【问题标题】:TypeError: Cannot read property 'get' of undefined (Node.js)TypeError:无法读取未定义的属性“获取”(Node.js)
【发布时间】:2017-02-06 22:49:00
【问题描述】:

我是 Node.js 的新手,我需要帮助。我正在尝试使用 express (^4.14.1)、路径 (^0.12.7) 和 vash (^0.12.2) Visual Studio 中的模块。我刚刚创建了几个文件,下面将向您展示。 homeController.jsindex.js 文件位于 controllers 文件夹中。

package.json

{
  "name": "the-board5",
  "version": "0.0.0",
  "description": "TheBoard5",
  "main": "server.js",
  "author": {
    "name": "Utku"
  },
  "dependencies": {
    "express": "^4.14.1",
    "path": "^0.12.7",
    "vash": "^0.12.2"
  }
}

server.js

var http = require("http");
var express = require("express");
var app = express();
var controllers = require("./controllers");
var port = process.env.port || 1337;

app.set("view engine", "vash");

controllers.init();

http.createServer(app).listen(port);

homeController.js

(function (homeController) {
    homeController.init = function (app) {
        app.get("/", function (req, res) {
            res.render("index", { title: "Express + Vash" });
        });
    };
})(module.exports);

index.js

(function (controllers) {
    var homeController = require("./homeController");
    controllers.init = function (app) {
        homeController.init(app);
    };
})(module.exports);

问题是我收到了这个错误。

C:\Users\Utku\documents\visual studio 2015\Projects\TheBoard5\TheBoard5\controll
ers\homeController.js:5
        app.get("/", function (req, res) {
           ^

TypeError: Cannot read property 'get' of undefined
    at Object.homeController.init (C:\Users\Utku\documents\visual studio 2015\Pr
ojects\TheBoard5\TheBoard5\controllers\homeController.js:5:12)
    at Object.controllers.init (C:\Users\Utku\documents\visual studio 2015\Proje
cts\TheBoard5\TheBoard5\controllers\index.js:5:24)
    at Object.<anonymous> (C:\Users\Utku\documents\visual studio 2015\Projects\T
heBoard5\TheBoard5\server.js:12:13)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)

【问题讨论】:

    标签: javascript asp.net node.js visual-studio express


    【解决方案1】:

    你错过了通过app 排队

    controllers.init();
    

    应该是

    controllers.init(app);
    

    【讨论】:

      猜你喜欢
      • 2020-05-15
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 2020-11-03
      • 2020-05-13
      相关资源
      最近更新 更多