【发布时间】:2023-03-20 13:51:02
【问题描述】:
我收到此错误:
next 不是函数
当我在控制器中写入next(); 时,即使我在函数参数中定义了错误,也无法弄清楚为什么会出现错误。
路由器 Js:
router.post('/era',controlleri.book,booking_controller.book,function(req,res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
console.log("done in server.js");
});
这是我的路由器文件,我想在其中同步执行功能,这就是我使用next();的原因。
Controller.Book功能代码:
module.exports.book=function(req,res,next){
var dates=req.body.dates;
var times=req.body.times;
var result="yes";
var query=`dates.${dates}.${times}.ava`;
db.mohd.find({[query]:result},{userid:1,_id:0},function (err, docs) {
if(err){
res.send(404);
}else if(docs==null){
res.send(404);
}else{
if(req.body){
var thid=req.body.therapist_id;
var dates=req.body.dates;
var times=req.body.times;
var query=`dates.${dates}.${times}.ava`;
db.mohd.update( { userid: thid },
{ $set: { [query]: "no" } }, function () {
console.log("updated");
});
**next();** //Here the error occurs
}else{
res.send("Get the F out of here");
}
}
});}
虽然执行了下一个函数,即booking_controller.book,但程序通过给出错误停止:
next 不是函数
【问题讨论】:
-
请正确缩进您的代码。这只是一场混乱。
-
我看不到你的函数参数中定义的
next。能否请您提供更多代码? -
抱歉添加了完整的正确代码@Pedro
-
尝试将
if(req.body){ ... else移到外面else