对于我的生活,我似乎无法找到解决方案。我得到的每个教程和资源似乎都没有帮助。我想要做的就是从我的MongoDB数据库中删除一个条目,但我似乎无法让Jade视图,数据库路由器和数据库做我想做的事情。基本上我希望能够从我的database.jade视图中删除一个条目并路由回它,而不是另一个视图或其他路由器。当我运行下面的代码时,我一直在尝试访问它的路由时遇到404错误。
我的app.js文件如下:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var router = express.Router();
var routes = require('./routes/index');
var users = require('./routes/users');
var database = require('./routes/database');
var methodOverride = require('method-override');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/public', express.static(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
app.use('public/js', express.static(path.join(__dirname + 'public/js')));
app.use('public/css', express.static(path.join(__dirname + 'public/css')));
app.use('/', routes);
app.use('/database', database);
app.use('/create', database);
app.use('/delete', database);
app.use('/:id', database);
app.use(methodOverride('_method'));
//delete an employee
app.use('/database/#{entry._id}?/delete', database);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
app.listen(8080);
console.log("The server has started");
我的database.js(这处理数据库视图的路由,我在其中输入条目,查看条目以及我要删除它们的位置)代码如下:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
/* Database stuff */
//connect to the data store and the set up the database
var db = mongoose.connection;
//connect to the database
mongoose.connect('mongodb://localhost/Mandela_Diaries/data');
//Create a model which connects to the schema and entries collection in the Mandela_Diaries database
var Entry = mongoose.model("Entry", new Schema({date: 'date', link: 'string'}), "entries");
mongoose.connection.on("open", function() {
console.log("mongodb is connected!");
});
//The route for getting data for the database - GET form
router.get("/", function(req, res) {
//Send the current entries to the page
Entry.find({}, function(err, entries) {
console.log(entries);
if(err) {
res.status(404).json({"error": "not found", "err":err});
return;
} else {
res.render('database', {title: 'database', entries: entries});
}
});
});
//The route for posting data to the database - POST
router.post('/', function(req, res) {
var newEntry = new Entry(req.body);
newEntry.save(function(err, entries){
if (err !== null) {
res.status(500).json(err);
} else {
res.redirect('database');
};
});
});
//The route for deleting data to the database - Delete/remove todo item by its id
// Changed path from `/` to `/:id`
router.delete('/:id', function (req, res) {
Entry.findById(req.params.id)
.exec(function(err, entries) {
// changed `if (err || !doc)` to `if (err || !entries)`
if (err || !entries) {
res.statusCode = 404;
res.send({});
} else {
entries.remove(function(err) {
if (err) {
res.statusCode = 403;
res.send(err);
} else {
res.send({});
}
});
}
});
});
module.exports = router;
我的数据库视图代码如下:
extends layout
block content
.container
.row
.col-s-12
h1 The Mandela Diaries Database
.row
.col-s-4
h3 Add Entry
.row
.col-s-12
form.form-inline(method='post', action='/create')
.form-group
label(for='date') Date:
input#datepicker.form-control.datepicker(type='text', name='date')
.form-group
label(for='link') Link:
input#link.form-control(type='string', name='link')
button.btn.btn-default(type='submit') Submit
.row
p
| Format options:
br
select#format
option(value='mm/dd/yy') Default - mm/dd/yy
option(value='yy-mm-dd') ISO 8601 - yy-mm-dd
option(value='d M, y') Short - d M, y
option(value='d MM, y') Medium - d MM, y
option(value='DD, d MM, yy') Full - DD, d MM, yy
option(value="'day' d 'of' MM 'in the year' yy") With text - 'day' d 'of' MM 'in the year' yy
br
ul#dbentries.col-s-12
each entry in entries
p= 'Date: ' + entry.date + ': Link: ' + entry.link + ' '
span
form(method='POST', action='/database/#{entry._id}?_method=DELETE')
button(type='submit') Delete
.row