每当我尝试访问"ReferenceError: next is not defined"
时,我都会遇到/catalog/bookinstance/:id/delete
。我花了差不多2个小时试图排除故障,但无济于事:(
关于哪个部分出错的任何建议?
catalog.js
/* GET request to delete BookInstance. */
router.get('/bookinstance/:id/delete', book_instance_controller.bookinstance_delete_get);
// POST request to delete BookInstance
router.post('/bookinstance/:id/delete', book_instance_controller.bookinstance_delete_post);
bookinstanceController.js
var BookInstance = require('../models/bookinstance');
var Book = require('../models/book');
var async = require('async');
// Display BookInstance delete form on GET
exports.bookinstance_delete_get = function(req, res, next) {
BookInstance.findById(req.params.id)
.populate('book')
.exec(function(err, bookinstance){
if(err){
return next(err);
}
res.render('bookinstance_delete', {title: 'Delete BookInstance', bookinstance: bookinstance})
});
};
// Handle BookInstance delete on POST
exports.bookinstance_delete_post = function(req, res, next) {
//req.checkBody('bookid', 'Book ID must exist').notEmpty();
BookInstance.findByIdAndRemove(req.body.id, function deleteBookInstance(err){
if(err){
return next(err);
}
res.redirect('/catalog/bookinstances');
});
};
bookinstance_delete.pug
h1= title
p #[strong Do you really want to delete this BookInstance?]
div(style="margin-left:20px")
p #[strong ID]: #{bookinstance._id}
p #[strong Title:]
a(href=bookinstance.book.url) #{bookinstance.book.title}
p #[strong Imprint:] #{bookinstance.imprint}
p #[strong Status:]
if bookinstance.status=='Available'
span.text-success #{bookinstance.status}
else if bookinstance.status=='Maintenance'
span.text-danger #{bookinstance.status}
else
span.text-warning #{bookinstance.status}
if bookinstance.status!='Available'
p #[strong Due back:] #{bookinstance.due_back_formatted}
form(method='POST' action='')
div.form-group
input#id.form-control(type='hidden',name='id', required='true', value=bookinstance._id )
button.btn.btn-primary(type='submit') Delete
bookinstance.js
var mongoose = require('mongoose');
var moment = require('moment');
var Schema = mongoose.Schema;
var BookInstanceSchema = Schema({
book: {type: Schema.ObjectId, ref: 'Book', required: true},
imprint: {type: String, required: true},
status: {type: String, required: true, enum: ['Available', 'Maintenance', 'Loaned', 'Reserved'], default: 'Maintenance'},
due_back: {type: Date, default: Date.now},
});
BookInstanceSchema
.virtual('url')
.get(function(){
return '/catalog/bookinstance/' + this._id;
});
BookInstanceSchema
.virtual('due_back_formatted')
.get(function(){
return moment(this.due_back).format('MMMM Do, YYYY');
});
module.exports = mongoose.model('BookInstance', BookInstanceSchema);