Error: Route.post() requires a callback function but got a [object Object]
Error: Route.post() requires a callback function but got a [object Object]
我正在尝试在我的 Web 应用程序中实现密码重置功能,但我一直在标题中收到错误消息。我的代码:
main.js:
const router = express.Router();
const AsyncNewPassword = require('./controller/asyncnewpassword');
const ResetPassWord = require ('./controller/resetpassword');
const ValidPasswordToken = require ('./controller/validpasswordtoken');
...
router.post('/req-reset-password', function (req, res) { ResetPassWord });
router.post('/new-password', function (req, res) { AsyncNewPassword });
router.post('/valid-password-token', function (req, res) { ValidPasswordToken });
asyncnewpassword.js:
module.exports.NewPassword = async function NewPassword(req, res) {
passwordResetToken.findOne({ resettoken: req.body.resettoken }, function (err, userToken, next) {
if (!userToken) {
return res
.status(409)
.json({ message: 'Token has expired' });
}
User.findOne({
_id: userToken._userId
}, function (err, userEmail, next) {
if (!userEmail) {
return res
.status(409)
.json({ message: 'User does not exist' });
}
return bcrypt.hash(req.body.newPassword, 10, (err, hash) => {
if (err) {
return res
.status(400)
.json({ message: 'Error hashing password' });
}
userEmail.password = hash;
userEmail.save(function (err) {
if (err) {
return res
.status(400)
.json({ message: 'Password can not reset.' });
} else {
userToken.remove();
return res
.status(201)
.json({ message: 'Password reset successfully' });
}
});
});
});
})
}
resetpassword.js:
module.exports.ResetPassword = async function ResetPassword(req, res) {
if (!req.body.email) {
return res
.status(500)
.json({ message: 'Email is required' });
}
const user = await User.findOne({
email:req.body.email
});
if (!user) {
return res
.status(409)
.json({ message: 'Email does not exist' });
}
var resettoken = new passwordResetToken({ _userId: user._id, resettoken: crypto.randomBytes(16).toString('hex') });
resettoken.save(function (err) {
if (err) { return res.status(500).send({ msg: err.message }); }
passwordResetToken.find({ _userId: user._id, resettoken: { $ne: resettoken.resettoken } }).remove().exec();
res.status(200).json({ message: 'Reset Password successfully.' });
var transporter = nodemailer.createTransport({
service: 'Gmail',
port: 465,
auth: {
user: 'user',
pass: 'password'
}
});
var mailOptions = {
to: user.email,
from: 'bot@example.com',
subject: 'Node.js Password Reset',
text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
'http://localhost:4200/response-reset-password/' + resettoken.resettoken + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n'
}
transporter.sendMail(mailOptions, (err, info) => {
})
})
}
validpasswordtoken.js:
module.exports.ValidPasswordToken = async function ValidPasswordToken(req, res) {
if (!req.body.resettoken) {
return res
.status(500)
.json({ message: 'Token is required' });
}
const user = await passwordResetToken.findOne({
resettoken: req.body.resettoken
});
if (!user) {
return res
.status(409)
.json({ message: 'Invalid URL' });
}
User.findOneAndUpdate({ _id: user._userId }).then(() => {
res.status(200).json({ message: 'Token verified successfully.' });
}).catch((err) => {
return res.status(500).send({ msg: err.message });
});
};
我的堆栈跟踪:
Error: Route.post() requires a callback function but got a [object Object]
at Route.<computed> [as post] (C:\Apps\Projects\Project1\backend\node_modules\express\lib\router\route.js:202:15)
at Function.proto.<computed> [as post] (C:\Apps\Projects\Project1\backend\node_modules\express\lib\router\index.js:510:19)
at Object.<anonymous> (C:\Apps\Projects\Project1\backend\index.js:441:8)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
最初代码在路由器第二个参数中没有函数包装器,但我添加了它,从那时起访问它时就找不到 URL...
你能试试吗:
router.post('/req-reset-password', ResetPassWord);
这应该调用方法ResetPassword
。
请注意:使用 resetPassword
.
正确的驼峰式大小写
编辑:
可能你的导入有问题。
尝试
module.exports = async function ResetPassword(req, res) {
...
否则用大括号导入:
const { ResetPassWord } = require ('./controller/resetpassword');
编辑 2:
既然你用的是nodejs,你可以用es6。
在依赖模块中:
exports.resetPassword = async (req, res) => {
...
在路由器文件中:
import { restPassword } from './controller/resetpassword';
应该可以,我一直在用。
我正在尝试在我的 Web 应用程序中实现密码重置功能,但我一直在标题中收到错误消息。我的代码:
main.js:
const router = express.Router();
const AsyncNewPassword = require('./controller/asyncnewpassword');
const ResetPassWord = require ('./controller/resetpassword');
const ValidPasswordToken = require ('./controller/validpasswordtoken');
...
router.post('/req-reset-password', function (req, res) { ResetPassWord });
router.post('/new-password', function (req, res) { AsyncNewPassword });
router.post('/valid-password-token', function (req, res) { ValidPasswordToken });
asyncnewpassword.js:
module.exports.NewPassword = async function NewPassword(req, res) {
passwordResetToken.findOne({ resettoken: req.body.resettoken }, function (err, userToken, next) {
if (!userToken) {
return res
.status(409)
.json({ message: 'Token has expired' });
}
User.findOne({
_id: userToken._userId
}, function (err, userEmail, next) {
if (!userEmail) {
return res
.status(409)
.json({ message: 'User does not exist' });
}
return bcrypt.hash(req.body.newPassword, 10, (err, hash) => {
if (err) {
return res
.status(400)
.json({ message: 'Error hashing password' });
}
userEmail.password = hash;
userEmail.save(function (err) {
if (err) {
return res
.status(400)
.json({ message: 'Password can not reset.' });
} else {
userToken.remove();
return res
.status(201)
.json({ message: 'Password reset successfully' });
}
});
});
});
})
}
resetpassword.js:
module.exports.ResetPassword = async function ResetPassword(req, res) {
if (!req.body.email) {
return res
.status(500)
.json({ message: 'Email is required' });
}
const user = await User.findOne({
email:req.body.email
});
if (!user) {
return res
.status(409)
.json({ message: 'Email does not exist' });
}
var resettoken = new passwordResetToken({ _userId: user._id, resettoken: crypto.randomBytes(16).toString('hex') });
resettoken.save(function (err) {
if (err) { return res.status(500).send({ msg: err.message }); }
passwordResetToken.find({ _userId: user._id, resettoken: { $ne: resettoken.resettoken } }).remove().exec();
res.status(200).json({ message: 'Reset Password successfully.' });
var transporter = nodemailer.createTransport({
service: 'Gmail',
port: 465,
auth: {
user: 'user',
pass: 'password'
}
});
var mailOptions = {
to: user.email,
from: 'bot@example.com',
subject: 'Node.js Password Reset',
text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
'http://localhost:4200/response-reset-password/' + resettoken.resettoken + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n'
}
transporter.sendMail(mailOptions, (err, info) => {
})
})
}
validpasswordtoken.js:
module.exports.ValidPasswordToken = async function ValidPasswordToken(req, res) {
if (!req.body.resettoken) {
return res
.status(500)
.json({ message: 'Token is required' });
}
const user = await passwordResetToken.findOne({
resettoken: req.body.resettoken
});
if (!user) {
return res
.status(409)
.json({ message: 'Invalid URL' });
}
User.findOneAndUpdate({ _id: user._userId }).then(() => {
res.status(200).json({ message: 'Token verified successfully.' });
}).catch((err) => {
return res.status(500).send({ msg: err.message });
});
};
我的堆栈跟踪:
Error: Route.post() requires a callback function but got a [object Object]
at Route.<computed> [as post] (C:\Apps\Projects\Project1\backend\node_modules\express\lib\router\route.js:202:15)
at Function.proto.<computed> [as post] (C:\Apps\Projects\Project1\backend\node_modules\express\lib\router\index.js:510:19)
at Object.<anonymous> (C:\Apps\Projects\Project1\backend\index.js:441:8)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
最初代码在路由器第二个参数中没有函数包装器,但我添加了它,从那时起访问它时就找不到 URL...
你能试试吗:
router.post('/req-reset-password', ResetPassWord);
这应该调用方法ResetPassword
。
请注意:使用 resetPassword
.
编辑:
可能你的导入有问题。
尝试
module.exports = async function ResetPassword(req, res) {
...
否则用大括号导入:
const { ResetPassWord } = require ('./controller/resetpassword');
编辑 2:
既然你用的是nodejs,你可以用es6。
在依赖模块中:
exports.resetPassword = async (req, res) => {
...
在路由器文件中:
import { restPassword } from './controller/resetpassword';
应该可以,我一直在用。