1. Prompt
when calling middlewareError: Route.get () requires a callback function but got a [object Undefined]
2. Add part of the code in the red box in the picture and report an error
3. Code file
server.js
var express = require("express");
domain = require("domain");
db = require("./db");
var order_search = require("./order_search");
var order_edit = require("./order_edit");
var cookieParser = require("cookie-parser");
var session = require("express-session");
var bodyParser = require("body-parser");
var app = express();
app.set("view engine", "ejs");
// app.use(cookieParser());
app.use(session({ resave: false, saveUninitialized: true, secret: "testApplication", cookie: { maxAge: 1200000 } }));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
// app.use(bodyParser());
// app.use(app.router);
app.use("/images", express.static(__dirname + "/images/"));
app.use(function (err, req, res, next) {
res.send(500, err.message);
});
app.get("/", order_search.get);
app.post("/search", order_search.search);
// app.get("/search", order_search.search);
app.get("/new", order_edit.new);
// app.get("/edit/:code", order_edit.search);
app.post("/edit", order_edit.add);
// app.post("/edit/:code", order_edit.update);
// app.post("/delete/:code", order_edit.delete);
app.get("/return", order_edit.return);
app.listen(80);
order_edit.js
exports.new = function(req, res){
res.render("order_edit", {code:"", errMsg:null, successMsg:null});
}
exports.search = function(req, res, next){
var d = domain.create();
d.on("error", function(err){
next(err);
});
d.run(function(){
db.connect(function(){
var order = {code:req.params.code};
db.searchOrder(order, function(){
db.disconnect();
res.render("order_edit", {code:req.body.code, order:db.orders[0], errMsg:null, successMsg:null});
})
})
})
}
exports.add = function(req, res, next){
var d = domain.create();
d.on("error", function(err){
next(err);
});
d.run(function(){
db.connect(function(){
var order = new Object();
order.code = req.body.code;
order.date = new Date(req.body.date);
order.goodsCode = req.body.goodsCode;
order.brandName = req.body.brandName;
order.num = req.body.num;
order.price = req.body.price;
order.personName = req.body.personName;
order.email = req.body.email;
db.addOrder(order, function(){
db.disconnect();
if(db.errMsg)
res.render("order_edit", {code:req.body.code, order:order, errMsg:db.errMsg, successMsg:null});
else
res.render("order_edit", {code:req.body.code, order:order, errMsg:null, successMsg:null});
})
})
})
exports.update = function(req, res, next){
var d = domain.create();
d.on("error", function(err){
next(err);
})
d.run(function(){
db.connect(function(){
var order = new Object();
order.code = req.body.code;
order.date = new Date(req.body.date);
order.goodsCode = req.body.goodsCode;
order.brandName = req.body.brandName;
order.num = req.body.num;
order.price = req.body.price;
order.personName = req.body.personName;
order.email = req.body.email;
db.upadteOrder(order, function(){
db.disconnect();
res.render("order_edit", {code:req.body.code, order:order, successMsg:"", errMsg:null});
})
})
})
}
exports.delete = function(req, res, next){
var d = domain.create();
d.on("error", function(err){
next(err);
});
d.run(function(){
db.connect(function(){
var order = new Object();
order.code = req.body.code;
db.deleteOrder(order, function(){
db.disconnect();
if(req.session.code||req.session.date||req.session.goodsCode)
res.redirect("/search/");
else
res.render("order_search", {rows:new Array(), order:null})
})
})
})
}
exports.return = function(req, res, next){
if(req.session.code||req.session.date||req.session.goodsCode)
{
res.redirect("/search/")
}
else{
res.render("order_search", {rows:new Array(), order:null})
}
}
}
4. Following the knock of the book, there is no problem with the other calls. As soon as this is added, it will make a mistake. I hope my friends can take a look at it.