产生创建错误:无法读取未定义的属性“长度”

时间:2020-03-31 19:54:26

标签: javascript node.js sequelize.js

当我尝试创建一个新的约会时,在提供者ID和日期内发送一个简单的json会引发以下错误:

(节点:13572)UnhandledPromiseRejectionWarning:TypeError:Cannot 读取未定义的属性“ length” 在Appointment._initValues(C:\ Users \ Usuario \ Desktop \ Studies \ Go_Stack \ modulo2 \ goBarber \ node_modules \ sequelize \ lib \ model.js:140:49) 在新模型上(C:\ Users \ Usuario \ Desktop \ Studies \ Go_Stack \ modulo2 \ goBarber \ node_modules \ sequelize \ lib \ model.js:118:10) 在新的约会中(C:\ Users \ Usuario \ Desktop \ Studies \ Go_Stack \ modulo2 \ goBarber \ src \ app \ models \ Appointment.js:3:1) 在Function.build(C:\ Users \ Usuario \ Desktop \ Studies \ Go_Stack \ modulo2 \ goBarber \ node_modules \ sequelize \ lib \ model.js:2167:12)中 在Function.create(C:\ Users \ Usuario \ Desktop \ Studies \ Go_Stack \ modulo2 \ goBarber \ node_modules \ sequelize \ lib \ model.js:2220:17) 在商店(C:\ Users \ Usuario \ Desktop \ Studies \ Go_Stack \ modulo2 \ goBarber \ src \ app \ controllers \ AppointmentController.js:65:57) (node:13572)UnhandledPromiseRejectionWarning:未处理的诺言 拒绝。该错误是由于将 没有catch块的异步函数,或者通过拒绝未使用.catch()处理的Promise。 (拒绝ID:1) (节点:13572)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

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 index(req, res) {
        const appointments = await Appointment.findAll({
            where: { user_id: req.userId, canceled_at: null }
        })

        return res.json(appointments);
    }

    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));

        console.log(hourStart);
        console.log(new Date());

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

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

        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();

1 个答案:

答案 0 :(得分:1)

这是您的配置中的问题。检查以下内容:

  1. 检查您与数据库的连接。
  2. 确保已初始化模型并且数据库表存在。