这个错误要么是在没有 catch 块的情况下在异步函数中抛出,要么是拒绝了一个没有处理的承诺
this error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with
我在尝试创建用户时遇到错误。 userValidation is not a function at exports.createUser
这是 validation.js 文件中的一个函数,它接受参数数据 body-VALUES
然后验证正文的每个输入。但我不知道为什么会出现此错误。
这里是 post 请求 /create-user
的回调函数
The requiring modules
const expressValidator = require('express-validator');
const bycrypt = require('bcryptjs');
const bodyparser = require("body-parser"); //requiring the body parser
const passport = require('passport');
const flash = require('express-session');
//import the validation file
const userValidation = require('./validation');
exports.createUser = async(req, res, next) => {
console.log('Post CREATE User /create-user');
//validation the user
const { error } = userValidation(req.body);
//if the req.body didn't pass the vaildation part
if (error) {
return res.status(400).send(error.details[0].message);
}
//解法之一
try {
//checking if the user is already exist
const userExist = await userSchema.findOne({ username: req.body.username });
if (userExist) return res.status(400).send('UserName Already Exist');
} catch (error) {
console.log(error);
}
//主要代码
//检查用户是否已经存在
// const userExist = await userSchema.findOne({ username: req.body.username });
// if (userExist) return res.status(400).send('UserName Already Exist');
//Hashing The Password
const salt = await bycrypt.genSalt(10);
const hashedPassword = await bycrypt.hash(req.body.password, salt);
// creating a new user from the schema
const user = new userSchema({
username: req.body.username,
password: req.body.hashedPassword
})
// saving the user inside the db
user
.save()
.then(data => {
res.redirect('/halalMunchies/all-employees');
})
.catch(err => {
res.status(500).send({
message: err.message || "Some error occured while creating a create operation"
});
});
};
验证文件,它与回调函数存在于同一文件夹中,用于验证用户的输入
//validation
const joi = require('@hapi/joi');
//creating user Schema validation
const userValidation = (data) => {
const schema = {
username: joi.string().min(6).required(),
password: joi.string().min(6).required()
};
return joi.validate(data, schema);
};
//creating login Schema validation
const loginValidation = (data) => {
const schema = {
username: joi.string().min(6).required(),
password: joi.string().min(6).required()
};
return joi.validate(data, schema);
};
//export
module.exports.userValidation = userValidation;
module.exports.loginValidation = loginValidation;
错误:
(node:28160) UnhandledPromiseRejectionWarning: TypeError: userValidation is not a function
at exports.createUser (E:\HalalMunchies\server\controller\usersController.js:32:23)
at Layer.handle [as handle_request] (E:\HalalMunchies\node_modules\express\lib\router\layer.js:95:5)
at next (E:\HalalMunchies\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\HalalMunchies\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\HalalMunchies\node_modules\express\lib\router\layer.js:95:5)
at E:\HalalMunchies\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\HalalMunchies\node_modules\express\lib\router\index.js:335:12)
at next (E:\HalalMunchies\node_modules\express\lib\router\index.js:275:10)
at Function.handle (E:\HalalMunchies\node_modules\express\lib\router\index.js:174:3)
at router (E:\HalalMunchies\node_modules\express\lib\router\index.js:47:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:28160) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting ck, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejecttps://nodejs.org/aptions=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:28160) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with ade.js process with a non-zero exit code.
THE NEXT ERROR
(node:5720) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'string' of undefined
at userValidation (E:\HalalMunchies\server\controller\validation.js:11:23)
at exports.createUser (E:\HalalMunchies\server\controller\usersController.js:32:23)
at Layer.handle [as handle_request] (E:\HalalMunchies\node_modules\express\lib\router\layer.js:95:5)
at next (E:\HalalMunchies\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\HalalMunchies\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\HalalMunchies\node_modules\express\lib\router\layer.js:95:5)
at E:\HalalMunchies\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\HalalMunchies\node_modules\express\lib\router\index.js:335:12)
at next (E:\HalalMunchies\node_modules\express\lib\router\index.js:275:10)
at Function.handle (E:\HalalMunchies\node_modules\express\lib\router\index.js:174:3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5720) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5720) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js
process with a non-zero exit code.
这个:
const userValidation = require('./validation');
不正确。根据您的验证文件,它需要是这样的:
const userValidation = require('./validation').userValidation;
或者这个:
const { userValidation } = require('./validation');
我在尝试创建用户时遇到错误。 userValidation is not a function at exports.createUser
这是 validation.js 文件中的一个函数,它接受参数数据 body-VALUES
然后验证正文的每个输入。但我不知道为什么会出现此错误。
这里是 post 请求 /create-user
的回调函数The requiring modules
const expressValidator = require('express-validator');
const bycrypt = require('bcryptjs');
const bodyparser = require("body-parser"); //requiring the body parser
const passport = require('passport');
const flash = require('express-session');
//import the validation file
const userValidation = require('./validation');
exports.createUser = async(req, res, next) => {
console.log('Post CREATE User /create-user');
//validation the user
const { error } = userValidation(req.body);
//if the req.body didn't pass the vaildation part
if (error) {
return res.status(400).send(error.details[0].message);
}
//解法之一
try {
//checking if the user is already exist
const userExist = await userSchema.findOne({ username: req.body.username });
if (userExist) return res.status(400).send('UserName Already Exist');
} catch (error) {
console.log(error);
}
//主要代码 //检查用户是否已经存在
// const userExist = await userSchema.findOne({ username: req.body.username });
// if (userExist) return res.status(400).send('UserName Already Exist');
//Hashing The Password
const salt = await bycrypt.genSalt(10);
const hashedPassword = await bycrypt.hash(req.body.password, salt);
// creating a new user from the schema
const user = new userSchema({
username: req.body.username,
password: req.body.hashedPassword
})
// saving the user inside the db
user
.save()
.then(data => {
res.redirect('/halalMunchies/all-employees');
})
.catch(err => {
res.status(500).send({
message: err.message || "Some error occured while creating a create operation"
});
});
};
验证文件,它与回调函数存在于同一文件夹中,用于验证用户的输入
//validation
const joi = require('@hapi/joi');
//creating user Schema validation
const userValidation = (data) => {
const schema = {
username: joi.string().min(6).required(),
password: joi.string().min(6).required()
};
return joi.validate(data, schema);
};
//creating login Schema validation
const loginValidation = (data) => {
const schema = {
username: joi.string().min(6).required(),
password: joi.string().min(6).required()
};
return joi.validate(data, schema);
};
//export
module.exports.userValidation = userValidation;
module.exports.loginValidation = loginValidation;
错误:
(node:28160) UnhandledPromiseRejectionWarning: TypeError: userValidation is not a function
at exports.createUser (E:\HalalMunchies\server\controller\usersController.js:32:23)
at Layer.handle [as handle_request] (E:\HalalMunchies\node_modules\express\lib\router\layer.js:95:5)
at next (E:\HalalMunchies\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\HalalMunchies\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\HalalMunchies\node_modules\express\lib\router\layer.js:95:5)
at E:\HalalMunchies\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\HalalMunchies\node_modules\express\lib\router\index.js:335:12)
at next (E:\HalalMunchies\node_modules\express\lib\router\index.js:275:10)
at Function.handle (E:\HalalMunchies\node_modules\express\lib\router\index.js:174:3)
at router (E:\HalalMunchies\node_modules\express\lib\router\index.js:47:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:28160) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting ck, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejecttps://nodejs.org/aptions=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:28160) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with ade.js process with a non-zero exit code.
THE NEXT ERROR
(node:5720) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'string' of undefined
at userValidation (E:\HalalMunchies\server\controller\validation.js:11:23)
at exports.createUser (E:\HalalMunchies\server\controller\usersController.js:32:23)
at Layer.handle [as handle_request] (E:\HalalMunchies\node_modules\express\lib\router\layer.js:95:5)
at next (E:\HalalMunchies\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\HalalMunchies\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\HalalMunchies\node_modules\express\lib\router\layer.js:95:5)
at E:\HalalMunchies\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\HalalMunchies\node_modules\express\lib\router\index.js:335:12)
at next (E:\HalalMunchies\node_modules\express\lib\router\index.js:275:10)
at Function.handle (E:\HalalMunchies\node_modules\express\lib\router\index.js:174:3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5720) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5720) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js
process with a non-zero exit code.
这个:
const userValidation = require('./validation');
不正确。根据您的验证文件,它需要是这样的:
const userValidation = require('./validation').userValidation;
或者这个:
const { userValidation } = require('./validation');