Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token return in ,while compiling ejs

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.

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

<!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('/');
  });

    });

sample insertd data from Mongodb

like image 937
Mesfin Avatar asked Mar 23 '17 11:03

Mesfin


2 Answers

Instead of

 <% include ../partials/head %>

Write

<%- include ("../partials/head") %>
like image 112
Justice Bringer Avatar answered Oct 04 '22 16:10

Justice Bringer


Add <%})%> before </table> or remove <!-- <% jsonData.forEach(function(users){%>--> because you don't close forEach

HTML comment <!-- --> don't affects ejs. You can use {# #} instead.

like image 28
ponury-kostek Avatar answered Oct 04 '22 14:10

ponury-kostek