【问题标题】:Getting error next is not a function in node js / express js接下来出现错误不是节点 js / express js 中的功能
【发布时间】: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

标签: node.js express next


【解决方案1】:

您需要将 next 作为 find 回调函数的第三个参数传递。

函数(错误,文档,下一个){ ... }

【讨论】:

    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2020-08-14
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    相关资源
    最近更新 更多