简体   繁体   English

我如何修复节点 UnhandledPromiseRejectionWarning

[英]How do i fix the node UnhandledPromiseRejectionWarning

When a try to send a simple json including the id of a provider and a date I get the following error当尝试发送一个包含提供者 ID 和日期的简单 json 时,我收到以下错误

UnhandledPromiseRejectionWarning: TypeError: Cannot
convert undefined or null to object
    at Function.keys (<anonymous>)
    at Function.findAll (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\node_modules\sequelize\lib\model.js:1692:47)
    at Function.findOne (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\node_modules\sequelize\lib\model.js:1924:17)
    at store (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\src\app\controllers\AppointmentController.js:28:63)
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
(node:19212) 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(). (rejection id: 1)
(node:19212) [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.


import * as Yup from 'yup';
import { startOfHour, parseISO, isBefore } from 'date-fns'; 
import User from '../models/user';
import Appointment from '../models/Appointment';


class AppointmentController {
    async store(req, res) {
        const schema = Yup.object().shape({
            provider_id: Yup.number().required(),
            date: Yup.date().required(),
        });

        if (!(await schema.isValid(req.body))) {
            return res.status(400).json({ error: 'Validations Fails' });
        }

        const { provider_id, date } = req.body;

        const checkIsProvider = await User.findOne({
            where: { id: provider_id, provider: true },
        });

        if (!checkIsProvider) {
            return res.status(400).json({ error:'You can only create appointments with providers' });
        }

        const hourStart = startOfHour(parseISO(date));

        if (isBefore(hourStart, new Date())) {
            return res.status(400).json({ error: 'Past dates are not permitted' });
        }

        const checkAvailability = await Appointment.findOne({
            where: {
                provider_id,
                canceled_at: null,
                date: hourStart,
            },
        });

        if (checkAvailability) {
            return res.status(400).json({ error: 'Appointment date is not available' });
        }

        const appointment = await Appointment.create({
            user_id: req.userId,
            provider_id,
            date,
        });

        return res.json(appointment);
    }
}

export default new AppointmentController();

findOne method in Appointment class raises exception and you do not catching it anywhere. Appointment 类中的 findOne 方法引发异常,您不会在任何地方捕获它。 You need to do something like that:你需要做这样的事情:

let checkAvailability;
try {
  checkAvailability = await Appointment.findOne({
    where: {
      provider_id,
      canceled_at: null,
      date: hourStart,
    },
  });
} catch (err) {
  checkAvailability = false
}

use try catch to handle exceptions.使用try catch处理异常。

Check out await reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await查看等待参考: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决? UnhandledPromiseRejectionWarning - how do i fix this? UnhandledPromiseRejectionWarning 如何修复 UnhandledPromiseRejectionWarning:错误:读取 ETIMEDOUT 和 UnhandledPromiseRejectionWarning:错误:写入 EPROTO 错误 - How do I fix UnhandledPromiseRejectionWarning: Error: read ETIMEDOUT and UnhandledPromiseRejectionWarning: Error: write EPROTO errors 我如何处理 Node.JS 中的 UnhandledPromiseRejectionWarning - How do I handle the UnhandledPromiseRejectionWarning in Node.JS 如何在 node.js 服务器中使用 Jest 解决 UnhandledPromiseRejectionWarning - How do I resolve a UnhandledPromiseRejectionWarning with Jest in node.js server 我该如何修复(节点:5796)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:错误? - How can i fix (node:5796) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: error? UnhandledPromiseRejectionWarning,如何解决? - UnhandledPromiseRejectionWarning, how to fix it? 如何修复控制台中的“UnhandledPromiseRejectionWarning” - How to fix 'UnhandledPromiseRejectionWarning' in console 如何避免节点中的 UnhandledPromiseRejectionWarning - How to avoid UnhandledPromiseRejectionWarning in Node 如何解决节点上的 UnhandledPromiseRejectionWarning? - how to solve UnhandledPromiseRejectionWarning on node? 如何修复“UnhandledPromiseRejectionWarning:ReferenceError:内容未定义” - How to fix “UnhandledPromiseRejectionWarning: ReferenceError: content is not defined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM