TypeError:无法读取未定义的属性“_locals”
Posted
技术标签:
【中文标题】TypeError:无法读取未定义的属性“_locals”【英文标题】:TypeError: Cannot read property '_locals' of undefined 【发布时间】:2017-05-18 09:57:12 【问题描述】:我正在尝试在 ubuntu 16.04 中使用 nodejs,并安装了 node 和 npm,但是当我尝试此操作时出现此错误“TypeError: Cannot read property '_locals' of undefined”:
var express = require("express");
app = express();
bodyParser = require("body-parser");
mongoose = require("mongoose");
app.set("view engine", "ejs");
app.get("/", function(req,res)
app.render("index");
);
app.listen(3000, function()
console.log("Server Started!");
)
在终端输出:
Server Started!
TypeError: Cannot read property '_locals' of undefined
at EventEmitter.render (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/application.js:548:11)
at /home/luis/Documents/work/webdevBootcamp/test/app.js:9:6
at Layer.handle [as handle_request] (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/layer.js:95:5)
at next (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/layer.js:95:5)
at /home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:330:12)
at next (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:271:10)
at expressInit (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/middleware/init.js:33:5)
at Layer.handle [as handle_request] (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:312:13)
at /home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:330:12)
at next (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:271:10)
at query (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/middleware/query.js:44:5)
当我加载 localhost:3000 时,它不会呈现 ejs 文件或让我使用我所做的 send() 函数
app.get("/", function(req,res)
app.send("whatever");
);
上面写着:
TypeError: app.send is not a function
我确实安装了 express 和 ejs 模块(运行 npm install i -S express ejs mongoose body-parser)
【问题讨论】:
能否请您查看***.com/questions/36013826/… 寻求帮助 你不应该调用 app.send 而不是 res.send。 app 是一个用于路由 http 请求的对象。 @digit 谢谢!哇,我觉得自己很愚蠢 【参考方案1】:请设置您的视图目录。 例如:如果你的索引模板文件在'src'中
app.set('views', __dirname + '/src');
【讨论】:
【参考方案2】:正如@digit 在 cmets 中所说: “你不应该调用 app.send 而不是 res.send。app 是一个用于路由 http 请求的对象。”
问题是我在做
app.get("/", function(req,res)
app.send("whatever");
);
而不是
app.get("/", function(req,res)
res.send("whatever"); // res instead of app
);
【讨论】:
以上是关于TypeError:无法读取未定义的属性“_locals”的主要内容,如果未能解决你的问题,请参考以下文章