javascript - 未处理的拒绝 SequelizeEagerLoadingError:电影与电影无关
问题描述
我正在尝试通过邮递员进行数据库 POST 和更新,但这会导致未处理的拒绝 SequelizeEagerLoadingError
使用数据库中的 actor.js 已尝试并成功,但目前使用电影数据库不成功
我的关联.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'
});
}
模型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'
});
};
路线电影.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;
查看 app.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;
解决方案
想办法为遇到类似问题的人发布答案。必须在路线 film.js 中重新组织我的 GET 请求
router.get('/', function(req, res, next) {
models.film
.findAll({
attributes: ['film_id', 'title', 'description', 'rental_rate', 'rating'],
})
.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), {
})
.then(filmsFound => {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(filmsFound));
})
});
推荐阅读
- javascript - navigationOptions 奇怪的行为
- prolog - 包含列表(列表 1,列表 2)
- r - 计算给定时间点的待处理任务数(最好使用 dplyr)
- javascript - NextJS-ReferenceError:元素未定义
- java - 使用 TupleQuery 的 SELECT 子句中的 Java JPA 标准查询子查询
- javascript - 如何在右侧部分动态添加html?
- java - 如何过滤字符串以仅包含允许的字符?
- riscv - 代码位置在 RISC-V 代码模型中是否重要?
- echarts - echarts:条形图从值开始?
- java - Java 套接字输入流块