为什么在节点中插入数据后没有正确的响应?
Why correct response not coming after data insert in Node?
我正在尝试使用 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);
});
});
};
这里不是 then
块,而是 catch
块被触发并抛出此错误:
{
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.
请帮帮我!!
根据您对常量的使用,您似乎应该在 .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);
}
};
我正在尝试使用 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);
});
});
};
这里不是 then
块,而是 catch
块被触发并抛出此错误:
{
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.
请帮帮我!!
根据您对常量的使用,您似乎应该在 .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);
}
};