Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Can't view Actors Table in DB browser

The newly created actors will print to the console, but when i refresh DB browser, only the movies db shows up without any actors. Will post code in comments.

App.js

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

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

  try {
    const movieInstances = await Promise.all([
    Movie.create({
      title: "The Big Lebowski",
      runtime: 117,
      releaseDate: "1998-03-06",
      isAvailableOnVHS: true,
    }),

    Movie.create({
      title: "There Will Be Blood",
      runtime: 158,
      releaseDate: "2008-01-25",
      isAvailableOnVHS: false,

    })
  ]);

  const actorInstances = await Promise.all([
  Actor.create({
    firstName: "Jeff",
    lastName: "Bridges",
  }),

  Actor.create({
    firstName: "Daniel",
    lastName: "Day Lewis",
  })
]);
  const actorsJSON = actorInstances.map(actor => actor.toJSON());
  console.log(actorsJSON);

  } catch (error) {
    if(error.name === 'SequelizeValidationError') {
      const errors = error.errors.map(err => err.message);
      console.error('Validation Errors:', errors);
    } else {
      throw error;
    }
  }
})();

index.js

const Sequelize = require('sequelize');

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


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

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

module.exports = db;

actor.js

const Sequelize = require('sequelize');

module.exports = (sequelize) => {
  class Actor extends Sequelize.Model {}
  Actor.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 Actor;
};

2 Answers

Promise.all() is going to return an array of promises in the order they were entered. You need to handle the returned promises before you can display the data.

Mark Westerweel
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Mark Westerweel
Full Stack JavaScript Techdegree Graduate 22,378 Points

I think I know what's up:

Go to the "Browse Data" tab next tot he "Database Structure" tab.

Switch Movies table to Actor table. You should see it now (and no longer the movies). You created another table, remember?

You see them both in the console, since you specifically printed them to both (node runs all console.log's and prints them). In the DB browser, this isn't the case (another interface).

thanks!