接下来怎么解决不是函数报错信息?
How to resolve next is not a function error message?
我正在尝试创建一个 MEAN 堆栈应用程序,我目前正在开发更新功能。
我的代码在遇到此方法时当前失败:
businessRoutes.route('/update/:id').post(function (req, res) {
Business.findById(req.params.id, function (err, business) {
if (!business)
return next(new Error('Could not load Document'));
else {
business.person_name = req.body.person_name;
business.business_name = req.body.business_name;
business.business_gst_number = req.body.business_gst_number;
business.save().then(business => {
res.json('Update complete');
console.log('Update Complete');
})
.catch(err => {
res.status(400).send("unable to update the database");
});
}
});
});
控制台中显示的错误消息是:
TypeError: next is not a function
这行代码失败了:
return next(new Error('Could not load Document'));
谁能告诉我为什么会发生这种情况以及我该如何解决它?
这可能是您的意思:
问题是你想成为 "next" 的参数实际上来自 express.js (这意味着它出现在你 运行 的第一个函数回调中(之后.post(
---这个函数---有3个参数你可以选择使用:
req
用户向您的服务器发出的请求
res
您正准备发送的回复
next
第三个参数是可选的,它允许您发送到下一个中间件,或者如果您向它发送一个参数,它将把它发送到错误处理程序,错误处理程序将发送您的错误作为响应。
另一方面:
您将 next
参数 运行domly 放置在您尝试调用的 mongoose 函数的中间:mongoose 函数实际上只接受 (err, item)...
businessRoutes.route('/update/:id').post(function (req, res, next) {
// note that express exposes the "next" param
// this is a callback indicating to run the `next` middleware
Business.findById(req.params.id, function (err, business, whoKnows) {
// note this is from mongoose.js: the callback function actually only has 2 parameters (err, and foundObject)
if (!business)
return next(new Error('Could not load Document'));
else {
business.person_name = req.body.person_name;
business.business_name = req.body.business_name;
business.business_gst_number = req.body.business_gst_number;
business.save().then(business => {
res.json('Update complete');
})
.catch(err => {
res.status(400).send("unable to update the database");
});
}
});
});
请注意,我建议使用 async await,这将使整个事情更容易理解:
businessRoutes.route('/update/:id').post(async (req, res, next) => {
try {
const foundBusiness = await Business.findById(req.params.id)
//... do stuff
catch (e) {
next(throw new Error(e))
// .. other stuff
}
})
findById
的第二个参数需要一个具有两个参数 err
和 <entity>
的回调。没有中间件或其他东西,你所说的 next(...)
会尝试调用你找到的实体。
来自文档
Adventure.findById(id, function (err, adventure) {});
你看,在你的情况下,business
总是未定义的,adventure
或 next
永远不是函数。
我正在尝试创建一个 MEAN 堆栈应用程序,我目前正在开发更新功能。
我的代码在遇到此方法时当前失败:
businessRoutes.route('/update/:id').post(function (req, res) {
Business.findById(req.params.id, function (err, business) {
if (!business)
return next(new Error('Could not load Document'));
else {
business.person_name = req.body.person_name;
business.business_name = req.body.business_name;
business.business_gst_number = req.body.business_gst_number;
business.save().then(business => {
res.json('Update complete');
console.log('Update Complete');
})
.catch(err => {
res.status(400).send("unable to update the database");
});
}
});
});
控制台中显示的错误消息是:
TypeError: next is not a function
这行代码失败了:
return next(new Error('Could not load Document'));
谁能告诉我为什么会发生这种情况以及我该如何解决它?
这可能是您的意思:
问题是你想成为 "next" 的参数实际上来自 express.js (这意味着它出现在你 运行 的第一个函数回调中(之后.post(
---这个函数---有3个参数你可以选择使用:
req
用户向您的服务器发出的请求res
您正准备发送的回复next
第三个参数是可选的,它允许您发送到下一个中间件,或者如果您向它发送一个参数,它将把它发送到错误处理程序,错误处理程序将发送您的错误作为响应。
另一方面:
您将 next
参数 运行domly 放置在您尝试调用的 mongoose 函数的中间:mongoose 函数实际上只接受 (err, item)...
businessRoutes.route('/update/:id').post(function (req, res, next) {
// note that express exposes the "next" param
// this is a callback indicating to run the `next` middleware
Business.findById(req.params.id, function (err, business, whoKnows) {
// note this is from mongoose.js: the callback function actually only has 2 parameters (err, and foundObject)
if (!business)
return next(new Error('Could not load Document'));
else {
business.person_name = req.body.person_name;
business.business_name = req.body.business_name;
business.business_gst_number = req.body.business_gst_number;
business.save().then(business => {
res.json('Update complete');
})
.catch(err => {
res.status(400).send("unable to update the database");
});
}
});
});
请注意,我建议使用 async await,这将使整个事情更容易理解:
businessRoutes.route('/update/:id').post(async (req, res, next) => {
try {
const foundBusiness = await Business.findById(req.params.id)
//... do stuff
catch (e) {
next(throw new Error(e))
// .. other stuff
}
})
findById
的第二个参数需要一个具有两个参数 err
和 <entity>
的回调。没有中间件或其他东西,你所说的 next(...)
会尝试调用你找到的实体。
来自文档
Adventure.findById(id, function (err, adventure) {});
你看,在你的情况下,business
总是未定义的,adventure
或 next
永远不是函数。