我正在尝试使用Node执行插入操作,但问题是数据被插入到数据库中,但也抛出了一个错误。下面是我的代码:
require('dotenv').config();
const db = require('../models/database');
const adminUser = db.AdminUser;
const errors = require('../config/errors').errors;
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
exports.signUp = (req, res, next) => {
console.log(req.body);
bcrypt.hash(req.body.password, 12).then(hash => {
const admin = new adminUser({
emailId: req.body.emailId,
firstName: req.body.firstName,
middleName: req.body.middleName,
lastName: req.body.lastName,
mobileNumber: req.body.mobileNumber,
userName: req.body.userName,
password: hash,
role: req.body.role,
createdBy: req.body.createdBy
});
admin.save().then(result => {
return res.status(200).send(error.OK);
}).catch(err => {
return res.status(500).send(error.SERVER_ERROR);
});
});
};这里,catch块被触发并抛出这个错误,而不是then块:
{
emailId: 'abc@gmail.c',
firstName: 'Avishek',
middleName: '',
lastName: 'Ray',
mobileNumber: '123-456-7890',
userName: 'avishek12',
password: 'abc123',
role: 'admin',
createdBy: 'avi'
}
Executing (default): INSERT INTO `admin_users` (`id`,`emailId`,`firstName`,`middleName`,`lastName`,`mobileNumber`,`userName`,`password`,`role`,`createdBy`,`createdAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP);
(node:17380) UnhandledPromiseRejectionWarning: ReferenceError: error is not defined
at D:\practice\otp\frontend\backend\controller\auth.controller.js:25:41
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:17380) 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:17380) [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.请帮帮我!!
发布于 2020-09-29 02:25:19
根据您对常量的使用,看起来您应该在.then()和.catch()中使用errors (带有S)。
const errors = require('../config/errors').errors;
// ... code
admin.save().then(result => {
// reference errors require
return res.status(200).send(errors.OK);
}).catch(err => {
// reference errors require
return res.status(500).send(errors.SERVER_ERROR);
});要使用async/await编写此代码,请执行以下操作
exports.signUp = async (req, res, next) => {
try {
const hash = await bcrypt.hash(req.body.password, 12);
const admin = new adminUser({
emailId: req.body.emailId,
firstName: req.body.firstName,
middleName: req.body.middleName,
lastName: req.body.lastName,
mobileNumber: req.body.mobileNumber,
userName: req.body.userName,
password: hash,
role: req.body.role,
createdBy: req.body.createdBy
});
await admin.save();
return res.status(200).send(errors.OK);
} catch (err) {
// console.log(err);
return res.status(500).send(errors.SERVER_ERROR);
}
};https://stackoverflow.com/questions/64106978
复制相似问题