0
votes

I've been trying to insert data through the upload page posting it to the index page. But all I get is the data just through the Terminal. This is de code:

app.js:

var express = require('express'),
    path = require('path'),
    session = require('express-session'),
    bodyParser = require('body-parser'),
    mysql = require('mysql'),
    myConnection = require('express-myconnection');

var dbOptions = {
    host: 'localhost',
    user: 'fcdeen_user',
    password: 'wEvGT9ctm8KUxdTD',
    database: 'fcdeen'
};

var indexRoutes = require('./routes/index'),    
    loginRoutes = require('./routes/login'),
    uploadRoutes = require('./routes/upload');

var app = express();

//Use session
 app.use(session({
   secret: "Its_A_veRy_DarK_SecreT",
   resave: false,
   saveUninitialized: true
 }));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.use(myConnection(mysql, dbOptions, 'single'));

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use('/index', indexRoutes);
app.use('/upload', uploadRoutes);

app.get('/', function(req, res) {
    req.getConnection(function(err, connection) {
        connection.query('SELECT * FROM pictures', function(err, results) {
            res.locals.pictures = results;
            console.log(results);
            res.render('pages/index', {pictures : results});
        });
    });
});

app.post('/upload', function(req, res) {
    req.getConnection(function (err, connection) {

    var name = req.body.name;
    var title = req.body.title;
    var location = req.body.location;
    var date = req.body.date;
    var image = req.body.image;

    console.log("post received: %s %s %s %s %s", name, title, location, date, image);

 connection.query("INSERT INTO pictures set ? ", [name, title, location, date, image], function(err, results) {
      res.redirect('/index');
         });
  });
});

//index page 
app.get('/', function(req, res) {
    res.render('pages/index');
 });

// index page 
app.get('/index', function(req, res) {
    res.render('pages/index');
});

// upload page 
app.get('/upload', function(req, res) {
    res.render('pages/upload');
});

// login page 
app.get('/login', function(req, res) {
    res.render('pages/login');
});

app.listen(8080);
console.log('Launch at port 8080');

index.ejs:

<!DOCTYPE html>
<html lang="en">
<head>
    <% include ../partials/head %>
</head>
<body class="container">

    <header>
        <% include ../partials/header %>
    </header>

    <main>
        <div class="jumbotron">
            <h1>Formula 1, see it, feel it!</h1></br>
            <p>Welcome! Here you can find the most spectacular pictures of the Formula 1</p>
        </div>
    </main>

    <div class="jumbotron">

           <% if(pictures.length){ %>     
    <table style="width:100%">
        <tr>
            <th>Name</th>
            <th>Title</th>
            <th>Location</th>
            <th>Date</th>
            <th>Rating</th>
        </tr>
            <% } %>
            <% for (var i = 0; i < pictures.length; i++) { %>
        <tr>

            <td><%= pictures[i].name %></td>
            <td><%= pictures[i].title %></td>
            <td><%= pictures[i].location %></td>
            <td><%= pictures[i].date %></td>
            <td><%= pictures[i].rating %></td>

            <% } %>
        </tr>                   
    </table>    

    </div>

    <footer>
        <% include ../partials/footer %>
    </footer>

</body>
</html>

upload.js

var express = require('express');
var router = express.Router();


router.get('/upload', function(req, res) {
  res.render('pages/upload');
});

router.post('/upload', function(req, res) {
  req.getConnection(function (err, connection) {
    var pictures = {
        // image     : req.body.images
        name     : req.body.name,
        title    : req.body.title,
        location : req.body.location,
        date     : req.body.date,
        rating   : req.body.rating,
    };
        console.log(req.body);

    connection.query("INSERT INTO pictures set ? ", [pictures], function(err, results) {
      res.redirect('/index');
    });
  });
});

module.exports = router;

upload.ejs:

<!DOCTYPE html>
<html lang="en">
<head>
    <% include ../partials/head %>
</head>
<body class="container">

    <header>
        <% include ../partials/header %>
    </header>

    <main>
        <div class="row">
            <div class="col-sm-8">

                <div class="jumbotron">
                    <h1>Upload your picture here</h1>
                </div>

            </div>
                <div>

                    <div class="col-sm-4">
                    <div class="well">

                        <form action="" method="post">

                            <label for="name">Name:</label>
                            <input type="text" name="name" id="name" value=""/>
                            </br>
                            <label for="title">Title:</label>
                            <input type="text" name="title" value=""/>
                            </br>
                            <label for="location">Location:</label>
                            <input type="text" name="location" id="location" value=""/>
                            </br>
                            <label for="date">Date:</label>
                            <input type="date" name="date" id="date" value=""/>
                            </br>
                            <label for="picture">Picture:</label>
                            <input type="file" name="picture" value=""/>
                            </br>
                            <label for="submit"></label>
                            <input type="submit" id="submit" value="Submit" />

                        </form>
                    </div>
                </div>

            </div>
        </div>
    </main>

    <footer>
        <% include ../partials/footer %>
    </footer>

</body>
</html>

Error msg:

Error: Failed to lookup view "/index" in views directory "C:\Users\Ferry\Desktop\SSS - Eindopdracht\views" at EventEmitter.render (C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\application.js:579:17) at ServerResponse.render (C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\response.js:961:7) at router.post.req.getConnection.pictures.name (C:\Users\Ferry\Desktop\SSS - Eindopdracht\routes\upload.js:6:7) at Layer.handle [as handle_request] (C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\router\layer.js:95:5) at C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\router\index.js:277:22 at Function.process_params (C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\router\index.js:330:12) at next (C:\Users\Ferry\Desktop\SSS - Eindopdracht\node_modules\express\lib\router\index.js:271:10)

1

1 Answers

0
votes

First it looks in like your routes you've defined "/upload/" as well as "/upload/upload/". Second, the syntax used in your sql query INSERT INTO pictures set ? uses a different syntax in both "/upload" and "/upload/upload/" I would change both.

// in index.js
app.post('/upload', function(req, res) {
req.getConnection(function (err, connection) {
var picture = {
    name : req.body.name,
    title : req.body.title,
    location : req.body.location,
    date : req.body.date,
    rating : req.body.rating,
    // image : req.body.image
    }

    console.log("post received: name: %s title: %s location: %s date: %s", picture.name, picture.title, picture.location, picture.date);

    connection.query("INSERT INTO pictures set ? ", picture, function(err, results) {
       console.log('Err it is:', err); // for checking
       console.log('results are in: ',results); // ^^ ditto
       res.redirect('/');
       });
    });
 });

and

// in upload.js
router.post('/', function(req, res) {
  req.getConnection(function (err, connection) {
     var picture = {
        // image     : req.body.images,
        name     : req.body.name,
        title    : req.body.title,
        location : req.body.location,
        date     : req.body.date,
        rating   : req.body.rating,
    };
       console.log(req.body);

     connection.query("INSERT INTO pictures set ? ", picture, function(err, results) {
        console.log("Err it is:", err);  // for checking
        console.log("Results are in:", results); // ^^ ditto

        res.redirect('/index');
     });
  });
});