我试图在我的应用中添加一个按钮,该按钮会删除一个条目(包含日期和链接),但似乎正在碰壁。当我单击按钮时出现此错误: Int Top = someAjax[1];
我想在视图中输入日期和链接(已经有效),查看条目(有效),然后删除条目并返回当前的观点(我遇到问题的地方)。
我的app.js文件如下所示:
Error 404 not found
我的database.js文件(处理GET,POST和DELETE操作)如下所示:
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 routes = require('./routes/index');
var users = require('./routes/users');
var database = require('./routes/database');
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('/users', users);
app.use('/database', database)
app.use('/create', 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.jade(我要路由到的地方)如下所示:
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
router.delete('/', function (req, res) {
Entry.findById(req.params.id)
.exec(function(err, entries) {
if (err || !doc) {
res.statusCode = 404;
res.send({});
} else {
entries.remove(function(err) {
if (err) {
res.statusCode = 403;
res.send(err);
} else {
res.send({});
}
});
}
});
});
我有一种感觉,我没有正确引用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: '
span
a(href="/delete/#{entries.id}") Delete
,但我可能错了。请帮忙: - (
答案 0 :(得分:3)
问题似乎是您未在路线中设置param
。此外,您还需要更改自己的html或jade,a(href="/delete/#{entries.id}") Delete
,这会向GET
发送/delete/{id}
请求,您需要发送{ {1}}请求DELETE
。
/database/{id}
至于你的玉,除非你通过Xhr做这个请求,否则你可能需要使用method override middleware。以下是玉器中表格的外观示例。
// 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({});
}
});
}
});
});
然后在 ul#dbentries.col-s-12
each entry in entries
p= 'Date: ' + entry.date + ': Link: '
span
form(method='POST', action='/database/#{entry._id}?_method=DELETE')
button(type='submit') Delete
理想情况下是第一个中间件之一。
app.js