我正在尝试开发一个存储用户名,国家和用户消息sin mongodb的留言簿应用程序,连接正常,我可以在数据库中提交这3个信息(用户名,国家和消息)。我的问题是将邮件转发到我的“guestbook.ejs”页面。
我很感激如果有人能就我遇到的问题给我一些线索。
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
<!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
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('/');
});
});
答案 0 :(得分:19)
代替
<% include ../partials/head %>
写
<%- include ("../partials/head") %>
答案 1 :(得分:5)
在<%})%>
之前添加</table>
或删除<!-- <% jsonData.forEach(function(users){%>-->
,因为您未关闭forEach
HTML评论<!-- -->
不会影响ejs。您可以改为使用{# #}
。
答案 2 :(得分:2)
就我而言,我发现包含以下部分模板的错误
<% include ./partials/messages %>
所以我用以下内容更改
<%- include ('partials/messages') %>
对我有用,希望对您有帮助
答案 3 :(得分:1)
删除代码中不必要的<%%>标记