gpt4 book ai didi

node.js - 接下来出现错误不是node js/express js中的函数

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:18 24 4
gpt4 key购买 nike

我收到此错误:

next is not a function

当我在 Controller 中编写 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");
}
);

// Here the error occurs
next();
} else {
res.send("Get the F out of here");
}
}
});
}

尽管执行了下一个函数,即 booking_controller.book ,但程序因给出错误而停止:

next is not a function

最佳答案

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

function(err, docs, next) { ... }

关于node.js - 接下来出现错误不是node js/express js中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com