Я пытаюсь получить базу данных POST и обновить ее через почтальона, но это приводит к необработанному отклонению. SequelizeEagerLoadingError
Это было предпринято и успешно с использованием actor.js из базы данных, но в настоящее время безуспешно с базой данных фильмов
My assocation.js:
module.exports = function(models) {
models.actor.belongsToMany(models.film,
{
through: models.film_actor,
foreignKey: 'actor_id'
});
models.film.belongsToMany(models.actor,
{
through: models.film_actor,
foreignKey: 'film_id'
});
}
models film.js:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('film', {
film_id: {
type: DataTypes.INTEGER(5).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
title: {
type: DataTypes.STRING(255),
allowNull: false
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
release_year: {
type: "YEAR(4)",
allowNull: true
},
language_id: {
type: DataTypes.INTEGER(3).UNSIGNED,
allowNull: false,
references: {
model: 'language',
key: 'language_id'
}
},
original_language_id: {
type: DataTypes.INTEGER(3).UNSIGNED,
allowNull: true,
references: {
model: 'language',
key: 'language_id'
}
},
rental_duration: {
type: DataTypes.INTEGER(3).UNSIGNED,
allowNull: false,
defaultValue: '3'
},
rental_rate: {
type: DataTypes.DECIMAL,
allowNull: false,
defaultValue: '4.99'
},
length: {
type: DataTypes.INTEGER(5).UNSIGNED,
allowNull: true
},
replacement_cost: {
type: DataTypes.DECIMAL,
allowNull: false,
defaultValue: '19.99'
},
rating: {
type: DataTypes.ENUM('G','PG','PG-13','R','NC-17'),
allowNull: true,
defaultValue: 'G'
},
special_features: {
type: "SET('TRAILERS','COMMENTARIES','DELETED SCENES','BEHIND THE SCENES')",
allowNull: true
},
last_update: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP')
}
}, {
tableName: 'film'
});
};
маршруты films.js:
const express = require('express');
var router = express.Router();
var models = require('../models/')
router.get('/', function(req, res, next) {
models.find
.findAll({
attributes: ['film_id','title','description', 'release_year']
})
.then(filmsFound => {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(filmsFound));
});
});
router.get('/:id', function(req, res, next) {
models.film
.findByPk(parseInt(req.params.id), {
include: [{ model: models.film }]
})
.then(filmsFound => {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(filmsFound));
})
});
router.post('/', function (req, res, next) {
models.actor.findOrCreate({
where: {
title: req.body.title
}
})
.spread(function(result, created) {
if (created) {
res.redirect('/films/' + result.film_id);
} else {
res.status(400);
res.send('Film already exists');
}
})
});
router.put("/:id", function (req, res, next) {
let filmId = parseInt(req.params.id);
models.film
.update(req.body, { where: { film_id: filmId } })
.then(result => res.redirect('/films/' + filmId))
.catch(err => {
res.status(400);
res.send("There was a problem updating the film. Please check the film information.");
});
});
router.delete("/:id", function (req, res, next) {
let filmId = parseInt(req.params.id);
models.film
.destroy({
where: { film_id: filmId }
})
.then(result => res.redirect('/films'))
.catch(err => {
res.status(400);
res.send("There was a problem deleting the film. Please make sure you are specifying the correct id.");
}
);
});
module.exports = router;
просмотр приложения.js:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var actorsRouter = require ('./routes/actors');
var filmsRouter = require('./routes/films');
var models = require('./models');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/actors', actorsRouter);
app.use('/films', filmsRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
models.sequelize.sync().then(function () {
console.log("DB Sync'd up")
});
module.exports = app;