gpt4 book ai didi

javascript - 类型错误 : Cannot read property '_locals' of undefined

转载 作者:行者123 更新时间:2023-11-30 08:28:29 24 4
gpt4 key购买 nike

我正在尝试在 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)

最佳答案

正如@digit 在评论中所说:“您不应该调用 app.send 而不是 res.send。应用程序是用于路由 http 请求的对象。”

问题是我在做

app.get("/", function(req,res){
app.send("whatever");
});

代替

app.get("/", function(req,res){
res.send("whatever"); // res instead of app
});

关于javascript - 类型错误 : Cannot read property '_locals' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41458240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com