我正在使用Node JS,为学校项目构建应用程序,其中图片必须由Mysql数据库上传,编辑和删除。我已经管理了上传页面,图片可以编辑,但我无法使用DELETE查询成功。在我的网页上,用户必须按删除按钮才能删除包含驱动程序,团队等数据的图片行。这是我的代码:
INDEX.EJS
<% if(pictures.length){ %>
<table style="width:100%">
<tr>
<th>Image</th>
<th>Driver</th>
<th>Team</th>
<th>Location</th>
<% if(admin) { %>
<th>Edit</th>
<th>Delete</th>
<% } %>
</tr>
<% } %>
<% for (var i = 0; i < pictures.length; i++) { %>
<tr>
<td><img src="images/<%= pictures[i].image %>" /></td>
<td><%= pictures[i].driver %></td>
<td><%= pictures[i].team %></td>
<td><%= pictures[i].location %></td>
<% if(admin) { %>
<td><a href="/edit/<%= pictures[i].id %>" >Edit</a></td>
<td><a href="/edit/delete/<%= pictures[i].id %>">Delete</a></td>
<% } %>
<% } %>
</tr>
</table>
</div>
EDIT.JS
router.get('/delete/:id', function(req, res) {
var id = req.params.id;
req.getConnection(function (err, connection) {
connection.query("DELETE FROM pictures WHERE id = ? ", [id], function(err, results) {
res.redirect('/');
console.log('Some data has been deleted')
});
});
});
APP.JS
var express = require('express'),
path = require('path'),
session = require('express-session'),
bodyParser = require('body-parser'), // Anders kan er geen POST worden gebruikt, de POST zit in de body
mysql = require('mysql'),
myConnection = require('express-myconnection');
// Set up de database
var dbOptions = {
host: 'localhost',
user: 'fcdeen_user',
password: 'wEvGT9ctm8KUxdTD',
database: 'fcdeen'
};
// Definieer de routers
var indexRoutes = require('./routes/index'),
loginRoutes = require('./routes/login'),
uploadRoutes = require('./routes/upload'),
editRoutes = require('./routes/edit');
// Applicatie wordt aangemaakt
var app = express();
// Gebruik de session
app.use(session({
secret: "Its_A_veRy_DarK_SecreT",
resave: false,
saveUninitialized: true
}));
// Voeg de bodyparser middleware aan de app toe, handelt de POST requests af
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Voeg connectie middleware toe aan app
app.use(myConnection(mysql, dbOptions, 'single'));
// Set de view engine op EJS
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Hierdoor weet express welke static file er gebruikt moet worden
app.use(express.static('public'));
// Verbind de Routers met de routes
app.use('/', indexRoutes);
app.use('/login', loginRoutes);
app.use('/upload', uploadRoutes);
app.use('/edit', editRoutes);
app.get('/', function(req, res) {
res.render('index');
});
// Start de server, App luistert naar poort 8080
app.listen(8080);
console.log('Launch at port 8080');
当我按下删除按钮时,我收到此错误:
无法GET / 10(10是我要删除的行的ID)