node.js 意外的令牌返回,同时编译 ejs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42974598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unexpected token return in ,while compiling ejs
提问by Mesfin
I am trying to develop a guestbook app which stores users names, country and users message sin mongodb, the connection is ok, I can submit these 3 infos(username, country and message)in the db. My Issue is to RENDER the messages into my "guestbook.ejs" page.
我正在尝试开发一个留言簿应用程序,它存储用户名、国家/地区和用户消息 sin mongodb,连接正常,我可以在 db 中提交这 3 个信息(用户名、国家/地区和消息)。我的问题是将消息渲染到我的“guestbook.ejs”页面中。
I would appreciate If anyone can give me a clue on the issue I encounter.
如果有人能就我遇到的问题给我一个线索,我将不胜感激。
SyntaxError: Unexpected token return in /Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/views/pages/guestbook.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
at Object.Function (<anonymous>)
at Object.Template.compile (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/ejs/lib/ejs.js:524:12)
at Object.compile (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/ejs/lib/ejs.js:338:16)
at handleCache (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/ejs/lib/ejs.js:181:18)
at tryHandleCache (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/ejs/lib/ejs.js:203:14)
at View.exports.renderFile [as engine] (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/ejs/lib/ejs.js:412:10)
at View.render (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/express/lib/view.js:126:8)
at tryRender (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/express/lib/application.js:639:10)
at EventEmitter.render (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/express/lib/application.js:591:3)
at ServerResponse.render (/Users/mesfint/Desktop/MEAN_DEV'T/Guestbook-application/node_modules/express/lib/response.js:960:7)
guestbook.ejs
留言簿.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<% include ../partials/head %>
</head>
<body class="container">
<header>
<% include ../partials/header %>
</header>
<main>
<div class="jumbotron">
<h4><%= guest_message %></h4>
<table border = "1">
<tr>
<th>Name</th>
<th>Country</th>
<th>Message</th>
</tr>
<!-- <% jsonData.forEach(function(users){%>-->
<% for(var i=0; i<newmessage.length; i++) {%>
<tr>
<td class="userInput"><%= newmessage[i].username %></td>
<td class="userInput"><%= newmessage[i].country %></td>
<td class="userInput"><%= newmessage[i].message %></td>
</tr>
<%} %>
</table>
</div>
</main>
<footer>
<% include ../partials/footer %>
</footer>
</body>
</html>
server.js
服务器.js
var express = require("express");
var bodyParser = require('body-parser');
var app = express();
var fs = require("fs");
var MongoClient = require('mongodb').MongoClient;
var db;
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static('public'));
MongoClient.connect('mongodb://mesfin:######@ds137090.mlab.com:37090/guestbook', function(err, database) {
if (err) return console.log(err)
db = database;
app.listen(3000, function () {
console.log('listening on 3000');
})
})
app.get("/", function(req,res){
res.render("pages/index", {
title_index: "What we speak?",
content_index:"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore",
footer_index:"My footer goes here"
});
});
app.get("/guestbook", function(req,res){
// res.render("pages/guestbook",{
db.collection('newmessage').find().toArray(function (err, result) {
if (err) return console.log(err)
res.render('pages/guestbook', {newmessage: result});
});
});
app.get("/newmessage", function(req,res){
res.sendFile(__dirname + "/pages/newmessage");
res.render("pages/newmessage",{
add_newMessage:"Add Your info & message!"
});
});
app.post("/newmessage" , function(req,res){
db.collection('newmessage').save(req.body, function(err, result) {
if (err) return console.log(err);
console.log('saved to database');
res.redirect('/');
});
});
回答by Justice Bringer
Instead of
代替
<% include ../partials/head %>
Write
写
<%- include ("../partials/head") %>
回答by ponury-kostek
Add <%})%>before </table>or remove <!-- <% jsonData.forEach(function(users){%>-->because you don't close forEach
添加<%})%>之前</table>或删除,<!-- <% jsonData.forEach(function(users){%>-->因为您没有关闭forEach
HTML comment <!-- -->don't affects ejs. You can use {# #}instead.
HTML 注释<!-- -->不影响 ejs。你可以{# #}改用。
回答by Joseph Stalin S
remove unnessary <% %> tags in your code
删除代码中不必要的 <% %> 标签
回答by Krishna Kumar Jangid
In my case, I found that error on include partial templates like below
就我而言,我发现包含部分模板的错误如下
<% include ./partials/messages %>
so I change it with following
所以我用以下方式改变它
<%- include ('partials/messages') %>
that's works for me, hope it helps
这对我有用,希望它有帮助


