NodeJS 编译ejs时意外返回标记

umuewwlo  于 2023-01-01  发布在  Node.js
关注(0)|答案(6)|浏览(141)

我正在尝试开发一个留言簿应用程序,其中存储用户名,国家和用户信息在mongodb,连接是好的,我可以提交这3个信息(用户名,国家和信息)在数据库中。我的问题是渲染到我的“留言簿。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('/');
  });

    });

xkrw2x1b

xkrw2x1b1#

代替

<% include ../partials/head %>

写入

<%- include ("../partials/head") %>
vpfxa7rd

vpfxa7rd2#

</table>之前添加<%})%>或删除<!-- <% jsonData.forEach(function(users){%>-->,因为您没有关闭forEach
HTML注解<!-- -->不影响ejs。您可以使用{# #}代替。

fnx2tebb

fnx2tebb3#

在我的例子中,我发现包含部分模板的错误如下

<% include ./partials/messages %>

所以我把它改成了

<%- include ('partials/messages') %>

这对我很有效,希望能有所帮助

ktecyv1j

ktecyv1j4#

删除代码中不必要的〈% %〉标记

sbtkgmzw

sbtkgmzw5#

在我的例子中,我在包含部分模板上发现了这个错误,如下所示〈% include .partials/messages %〉,所以我将其更改为〈%- include(“.partials/messages”)%〉

fiei3ece

fiei3ece6#

问题出在你的节点路由中。在你的/newMessage路由重写中

res.render("pages/newmessage",

res.render("/pages/newmessage",

同样在/guestbook路由重写中

res.render("pages/guestbook",

res.render("/pages/guestbook",

请尝试查看有关节点路由https://dev.to/ericchapman/nodejs-express-part-5-routes-and-controllers-55d3的文章

相关问题