I can’t return any information or send it to the database (javascript, Node, sequelize, postgres)

Asked

Viewed 43 times

0

Good afternoon, you guys! I am doing this project of registering classes with login screen, admin user etc. and I am currently assembling the back end of the project. The goal is to create a day and time registration for the class and its status (available or postponed), along with its id. The problem is that when trying to register (POST) the information with JSON in India, I am not returned anything, not even an error (I tried to log the error of Try/catch and nothing is returned either).

table creation in the database:

'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('classes', {
      id: {
        type: Sequelize.UUID,
        allowNull: false,
        autoIncrement: false,
        primaryKey: true,
      },
      date: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      hour: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      status: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });
  },
  down: async (queryInterface) => {
    await queryInterface.dropTable('classes');
  },
};

Model of the class:

import Sequelize, { Model } from 'sequelize';

class Class extends Model {
  static init(sequelize){
    super.init(
      {
        id: {
          type: Sequelize.UUID,
          defaultValue: Sequelize.UUIDV4,
          primaryKey: true,
        },
        date: Sequelize.STRING,
            hour: Sequelize.STRING,
            status: Sequelize.STRING,
      },
      {
        sequelize,
      }
    );
    return this;
  }
}

export default Class;

Class controller:

import * as Yup from 'yup';
import Class from '../models/Class';

class ClassController {
  async index(req,res){
    try {
      const classes = await Class.findAll({
        attributes: ['id', 'date', 'hour','status'],
      });

      return res.json(classes);
    } catch (error) {
      return res.json(error);
    }
  }

  async store(req,res){
    try {
      const schema = Yup.object().shape({
        date: Yup.string().required(),
        hour: Yup.string().required(),
        status: Yup.string().required(),
      });

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

      const { id, date, hour, status } = await Class.create(req.body);
      return res.json({
        id,
        date,
        hour,
        status,
      });
    } catch (error) {
      return res.json(error);
    }
  }

}

export default new ClassController();
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.