Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Using SQL ORMs with Node.js Defining Models Exercise: Add a Person Model

Validation error for empty string in person firstName and lastName

This is frustrating. Everything seems to be correct but I still get validation errors. At some point, I just replaced my code with the teachers' code, but I have the same error.

App.js :

const db = require('./db');
const { Movie, Person } = db.models;

(async () => {
    //syncs all tables
    await db.sequelize.sync({ force: true });

    try {
        const movie = await Movie.create({
            title: 'Toy Story',
            runtime: 81,
            releaseDate: '1995-11-22',
            isAvailableOnVHS: true
        });
        console.log(movie.toJSON());
        const movie2 = await Movie.create({
            title: 'The martian',
            runtime: 115,
            releaseDate: '2004-04-14',
            isAvailableOnVHS: true
        });
        console.log(movie2.toJSON());
        const person = await Person.create({
            firstName: 'Tom',
            lastName: 'Hanks'
        });
        console.log(person.toJSON());
    } catch (error) {
        if (error.name === 'SequelizeValidationError') {
            const errors = error.errors.map(err => err.message);
            console.error('Validation errors:', errors);
        } else {
            throw error;
        }
    }
})();

person.js:

const Sequelize = require('sequelize');

module.exports = sequelize => {
    class Person extends Sequelize.Model {}
    Person.init(
        {
            firstName: {
                type: Sequelize.STRING,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: 'Please provide a value for "firstName"'
                    },
                    notEmpty: {
                        msg: 'Please provide a value for "firstName"'
                    }
                }
            },
            lastName: {
                type: Sequelize.STRING,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: 'Please provide a value for "lastName"'
                    },
                    notEmpty: {
                        msg: 'Please provide a value for "lastName"'
                    }
                }
            }
        },
        { sequelize }
    );
    return Person;
};

I get this error message:

> node app.js

(node:4738) [SEQUELIZE0002] DeprecationWarning: The logging-option should be either a function or false. Default: console.log
Executing (default): DROP TABLE IF EXISTS `People`;
Executing (default): DROP TABLE IF EXISTS `Movies`;
Executing (default): DROP TABLE IF EXISTS `Movies`;
Executing (default): CREATE TABLE IF NOT EXISTS `Movies` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(255) NOT NULL, `runtime` INTEGER NOT NULL, `releaseDate` DATE NOT NULL, `isAvailableOnVHS` TINYINT(1) NOT NULL DEFAULT 0, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`Movies`)
Executing (default): DROP TABLE IF EXISTS `People`;
Executing (default): CREATE TABLE IF NOT EXISTS `People` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `firstName` VARCHAR(255) NOT NULL, `lastName` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`People`)
Validation errors: [
  'Please provide a value for "firstName"',
  'Please provide a value for "lastName"'
]

1 Answer

you forget to import person.js in index.js file.

const Sequelize = require('sequelize');

const sequelize = new Sequelize({
    dialect: 'sqlite',
    storage: 'movies.db',
});

const db = {
    sequelize,
    Sequelize,
    models: {},
};

db.models.Movie = require('./models/movie')(sequelize);

// remember to add this line otherwise you will have validation error
db.models.Person = require('./models/person')(sequelize);

module.exports = db;