简体   繁体   English

TypeError:无法读取未定义的属性“ get”(Node.js)

[英]TypeError: Cannot read property 'get' of undefined (Node.js)

I am new in Node.js and I need help. 我是Node.js的新手,需要帮助。 I am trying to develop a simple program using express (^4.14.1) , path (^0.12.7) and vash (^0.12.2) modules in Visual Studio. 我正在尝试在Visual Studio中使用express (^ 4.14.1) ,path (^ 0.12.7)和vash (^ 0.12.2)模块开发一个简单的程序。 I have just created a couple of files that I show you below. 我刚刚创建了几个文件,下面向您展示。 homeController.js and index.js files are inside the controllers folder. homeController.jsindex.js文件位于controllers文件夹中。

package.json 的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 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 homeController.js

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

index.js index.js

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

The problem is that I am getting this error. 问题是我收到此错误。

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)

You missed passing app in line 您错过了排队通过的app

controllers.init();

It should be 它应该是

controllers.init(app);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js 抛出 TypeError:无法读取未定义的属性“testModule” - Node.js throwing TypeError: Cannot read property 'testModule' of undefined TypeError:无法读取node.js中未定义的属性“名称” - TypeError: Cannot read property 'name' of undefined in node.js Node.js UnhandledPromiseRejection:TypeError:无法读取未定义的属性“ sign” - Node.js UnhandledPromiseRejection: TypeError: Cannot read property 'sign' of undefined TypeError:无法读取Node.js中未定义的属性“ length” - TypeError: Cannot read property 'length' of undefined in Node.js Node.js -&gt; TypeError:无法读取上下文中未定义的属性“then” - Node.js -> TypeError: Cannot read property 'then' of undefined at Context TypeError:无法读取未定义(Promise)(node.js)的属性“ then” - TypeError: Cannot read property 'then' of undefined (Promise) (node.js) 未捕获的类型错误:无法读取 node.js 上未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined on node.js Node.js - 类型错误:无法读取未定义的属性“exec” - Node.js - TypeError: Cannot read property 'exec' of undefined Node.js TypeError:无法读取未定义的属性“主机” - Node.js TypeError: Cannot read property 'host' of undefined 类型错误:无法读取未定义的 Node.js 的属性 - TypeError: Cannot read property of undefined Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM