Reputation: 55
When I try to create a new Appointment sending a simple json within a provider id and a date it raises the following error:
(node:13572) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined at Appointment._initValues (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\node_modules\sequelize\lib\model.js:140:49) at new Model (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\node_modules\sequelize\lib\model.js:118:10) at new Appointment (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\src\app\models\Appointment.js:3:1) at Function.build (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\node_modules\sequelize\lib\model.js:2167:12) at Function.create (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\node_modules\sequelize\lib\model.js:2220:17) at store (C:\Users\Usuario\Desktop\Studies\Go_Stack\modulo2\goBarber\src\app\controllers\AppointmentController.js:65:57) (node:13572) 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:13572) [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 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();
Upvotes: 1
Views: 1479
Reputation: 3253
It's a problem in your configurations. Check below:
Upvotes: 1