我正在使用 ejs 和 mongoose 制作一个 express 应用程序。
我收到这个错误:
Error: Failed to lookup view "error" in views directory "/Users/ben/Documents/csMSc/web/site/app/views"
at EventEmitter.app.render (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/application.js:555:17)
at ServerResponse.res.render (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:938:7)
at module.exports (/Users/ben/Documents/csMSc/web/site/app/app.js:94:7)
at Layer.handle_error (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/layer.js:58:5)
at trim_prefix (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:300:13)
at /Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:321:12)
at IncomingMessage.next (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:261:10)
at fn (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:933:25)
at EventEmitter.app.render (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/application.js:557:14)
at ServerResponse.res.render (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:938:7)
at app.use.res.render.message (/Users/ben/Documents/csMSc/web/site/app/app.js:83:9)
at Layer.handle_error (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/layer.js:58:5)
at trim_prefix (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:300:13)
at /Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/ben/Documents/csMSsc/web/site/app/node_modules/express/lib/router/index.js:321:12)
从对 res.render() 的两次调用中,传入的数据从 mongoose 查询返回,例如:
if(req.query.author !== undefined) {
var author = req.query.author;
Post.find().where('author').equals(author).sort({ created: -1 }).limit(10).exec(function(err, authorsPosts) {
if (err) return res.send("error");
if(authorsPosts.length==0) {
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: authorsPosts,
error: "Sorry there are no posts with that tag."
});
} else {
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: authorsPosts
});
}
});
}
另一个是相同的,但查询为
Post.find( { tags : { $elemMatch: { $in : tagList } } } ).limit(10).exec(function(err, taggedPosts) {
但是我在这个 View 上的所有其他渲染调用都工作正常,包括稍后在同一函数中的调用:
//or just latest
Post.find().sort({ created: 1 }).limit(10).exec(function(err, latestPosts) {
if (err) return res.send(err);
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: latestPosts
});
});
我很确定 latestPosts 的格式与上面的 authorsPosts 完全相同。
没有调用来呈现名为错误的 View 。
传递给上面对 res.render('pages/index') 的一些调用的错误数据是使用自定义过滤器传递的
//custom ejs filter, sets to default value if data not supplied
ejs.filters.get = function(obj, prop, def) {
return obj[prop] === undefined ? def : obj[prop];
};
在文件 app/views/pages/index.ejs 中显示为
<p><%=: locals | get:'error','' %> </p>
我的 ejs 设置如下:
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('ejs', require('ejs').renderFile);
这是整个令人讨厌的路线功能的可怕荣耀
router.get('/', function(req, res, next) {
var accountController = new AccountController(User, req.session);
console.log("here1");
var signedIn = accountController.session.userProfileModel !== undefined ? 1 : 0;
console.log("here2");
//Author search
if(req.query.author !== undefined) {
var author = req.query.author;
Post.find().where('author').equals(author).sort({ created: -1 }).limit(10).exec(function(err, authorsPosts) {
if (err) return res.send("error");
console.log("\n\nAuthorsPosts:" +authorsPosts);
console.log("\n\authorsPosts.length: " +authorsPosts.length);
console.log("authors post.constructor = " +authorsPosts.constructor);
if(authorsPosts.length==0) {
console.log("length=0");
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: authorsPosts,
error: "Sorry there are no posts with that tag."
});
} else {
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: authorsPosts
});
}
});
}
//Tag search
if(req.query.filter !== undefined) {
var tagList = req.query.filter.constructor == Array ? req.query.filter : req.query.filter.split(",");
Post.find( { tags : { $elemMatch: { $in : tagList } } } ).limit(10).exec(function(err, taggedPosts) {
if (err) return res.send("error");
console.log("\n\taggedPosts.length: " +taggedPosts.length);
if(taggedPosts.length==0) {
console.log("length=0");
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: taggedPosts,
error: "Sorry there are no posts with that tag."
});
} else {
console.log("\n\ntaggedPosts:\n\n" +taggedPosts);
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: taggedPosts
});
}
});
}
//or just latest
Post.find().sort({ created: 1 }).limit(10).exec(function(err, latestPosts) {
if (err) return res.send(err);
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: latestPosts
});
});
});
更重要的是,它并非完全不起作用。当代码到达那些呈现调用时,它会抛出错误并且页面通常会卡住,您无法单击任何链接,然后如果我重新加载一次或两次它就会工作,并使用正确的数据呈现模板。
此外,当我使用这些查询字符串之一前往“/”时,例如GET/?filter=Marc%20Behrens 它尽可能打印所有返回的帖子,然后抛出错误。
谢谢!
编辑:感谢 Alex Ford。
新错误是:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.header (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:700:10)
at ServerResponse.send (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:154:12)
at ServerResponse.json (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:240:15)
at ServerResponse.send (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:142:21)
at module.exports (/Users/ben/Documents/csMSc/web/site/app/app.js:100:9)
at Layer.handle_error (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/layer.js:58:5)
at trim_prefix (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:300:13)
at /Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:321:12)
at next (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:261:10)
at Layer.handle_error (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/layer.js:60:5)
at trim_prefix (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:300:13)
at /Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:321:12)
at next (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:261:10)
最佳答案
我对“headers already sent”错误的猜测是,即使上述 if 语句之一运行,您的 //或最新的 代码仍在运行。如果是这种情况,那么您肯定会多次调用 res.render 或 res.send。试试这个:
router.get('/', function(req, res, next) {
/* ... */
//Author search
if(req.query.author !== undefined) {
/* ... */
if(authorsPosts.length==0) {
res.render(/*...*/);
} else {
res.render(/*...*/);
}
/* ... */
}
//Tag search
else if(req.query.filter !== undefined) {
/* ... */
if(taggedPosts.length==0) {
res.render(/*...*/);
} else {
res.render(/*...*/);
}
/* ... */
}
//or just latest
else {
res.render(/*...*/);
}
});
关于node.js - Node express ejs 错误 : Failed to lookup view "error" in views directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30651121/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby常量:Content2而不是content2。Aconstantnamestart
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe