gpt4 book ai didi

javascript - 未处理的拒绝 SequelizeEagerLoadingError : film is not associated to film

转载 作者:行者123 更新时间:2023-11-29 15:30:24 29 4
gpt4 key购买 nike

我正在尝试通过 postman 进行数据库 POST 和更新,但这会导致未处理的拒绝 SequelizeEagerLoadingError

使用数据库中的 actor.js 已尝试并成功,但目前在电影数据库中未成功

我的association.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 - 未处理的拒绝 SequelizeEagerLoadingError : film is not associated to film,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58775420/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com