首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在节点中插入数据后没有出现正确的响应?

为什么在节点中插入数据后没有出现正确的响应?
EN

Stack Overflow用户
提问于 2020-09-29 01:36:17
回答 1查看 28关注 0票数 0

我正在尝试使用Node执行插入操作,但问题是数据被插入到数据库中,但也抛出了一个错误。下面是我的代码:

代码语言:javascript
复制
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块:

代码语言:javascript
复制
{
  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.

请帮帮我!!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-29 02:25:19

根据您对常量的使用,看起来您应该在.then().catch()中使用errors (带有S)。

代码语言:javascript
复制
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编写此代码,请执行以下操作

代码语言:javascript
复制
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);
    }
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64106978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档