如何在控制器中使用 Sequelize 与 .then 的关联
How to use Sequelize associations with .then in controller
这是我在 toWatch-model.js 文件中的 ToWatch 模型,它在代码中具有 UserModel->ToWatch 1:1 关系和 ToWatch->MovieModel 1:M 关系。
const Sequelize = require('sequelize');
const sequelize = require('./../common/db-config');
const MovieModel = require ("./movie-model");
const UserModel = require("./user-model");
const Model = Sequelize.Model;
class ToWatch extends Model{}
ToWatch.init({
id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'id_towatch'
},
date: {
type: Sequelize.DATE,
allowNull: false,
field: 'date'
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
field: 'id_user',
references:{
model: UserModel,
key: "id"
}
},
movieId: {
type: Sequelize.INTEGER,
allowNull: false,
field: 'movie_id_towatch',
references:{
model: MovieModel,
key: "id"
}
},
},
{
sequelize,
modelName: 'towatch',
tableName: 'towatch',
timestamps: false
// options
});
//Here is the relation ************
UserModel.hasOne(ToWatch, {
foreignKey: {
type:Sequelize.DataTypes.INTEGER,
allowNull:false,
name:'fk_foreign_key_towatch'
}
});
ToWatch.belongsTo(UserModel);
ToWatch.hasMany(MovieModel, {
foreignKey: {
type:Sequelize.DataTypes.INTEGER,
allowNull:false,
name:'movie_id_towatch'
}
});
MovieModel.belongsTo(ToWatch);
module.exports = ToWatch;
我看了很多教程,但这是我第一次尝试制作一种方法来 return 一切,包括通过 ID 从我的另一个 table 获得的东西,我不确定放在哪里如何将我需要的数据放入此方法中,考虑到它有 .then(data=>res.send)
。教程通过获取或使用 async-await 以其他方式完成,甚至文档在这里也没有帮助我。有人能告诉我在这个方法中放什么和放什么地方吗,在 toWatch-controller.js 文件中,我可以看到所有的电影数据(标题、img、日期),作为一个数组,我认为, getToWatch 方法。
const ToWatch = require('./../models/toWatch-model');
module.exports.getToWatch = (req,res) => {
ToWatch.findAll().then(toWatch => {
[WHAT DO I PUT HERE?]
res.send(toWatch);
}).catch(err => {
res.send({
status: -1,
error:err
})
})
}
我需要这样的 ToWatch{
color:red,
电影院:“MoviePlace”,
movieId{title:"安娜贝尔", img:"URL", date:1999.02.23}
据我正确理解您的问题,您尝试做的是 return toWatch 模型实例,包括 User 和 Movie 模型。
为此,您可以在 findAll() 方法中传递选项,如下所示:
ToWatch.findAll({include: [{model: User}, {model: Movie}]})... //and rest of your code
或者为了保持代码整洁,您可以使用作用域:
// below the toWatch-model.js file
ToWatch.addScope('userIncluded', {include: {model: User}})
ToWatch.addScope('movieIncluded', {include: {model: Movie}})
并且在 getToWatch 方法中:
// you can pass several scopes inside scopes method, its usefull when you want to combine query options
ToWatch.scopes('userIncluded', 'movieIncluded').findAll()... //rest of your code
有关更多信息,请查看 sequelize 文档
这是我在 toWatch-model.js 文件中的 ToWatch 模型,它在代码中具有 UserModel->ToWatch 1:1 关系和 ToWatch->MovieModel 1:M 关系。
const Sequelize = require('sequelize');
const sequelize = require('./../common/db-config');
const MovieModel = require ("./movie-model");
const UserModel = require("./user-model");
const Model = Sequelize.Model;
class ToWatch extends Model{}
ToWatch.init({
id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'id_towatch'
},
date: {
type: Sequelize.DATE,
allowNull: false,
field: 'date'
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
field: 'id_user',
references:{
model: UserModel,
key: "id"
}
},
movieId: {
type: Sequelize.INTEGER,
allowNull: false,
field: 'movie_id_towatch',
references:{
model: MovieModel,
key: "id"
}
},
},
{
sequelize,
modelName: 'towatch',
tableName: 'towatch',
timestamps: false
// options
});
//Here is the relation ************
UserModel.hasOne(ToWatch, {
foreignKey: {
type:Sequelize.DataTypes.INTEGER,
allowNull:false,
name:'fk_foreign_key_towatch'
}
});
ToWatch.belongsTo(UserModel);
ToWatch.hasMany(MovieModel, {
foreignKey: {
type:Sequelize.DataTypes.INTEGER,
allowNull:false,
name:'movie_id_towatch'
}
});
MovieModel.belongsTo(ToWatch);
module.exports = ToWatch;
我看了很多教程,但这是我第一次尝试制作一种方法来 return 一切,包括通过 ID 从我的另一个 table 获得的东西,我不确定放在哪里如何将我需要的数据放入此方法中,考虑到它有 .then(data=>res.send)
。教程通过获取或使用 async-await 以其他方式完成,甚至文档在这里也没有帮助我。有人能告诉我在这个方法中放什么和放什么地方吗,在 toWatch-controller.js 文件中,我可以看到所有的电影数据(标题、img、日期),作为一个数组,我认为, getToWatch 方法。
const ToWatch = require('./../models/toWatch-model');
module.exports.getToWatch = (req,res) => {
ToWatch.findAll().then(toWatch => {
[WHAT DO I PUT HERE?]
res.send(toWatch);
}).catch(err => {
res.send({
status: -1,
error:err
})
})
}
我需要这样的 ToWatch{ color:red, 电影院:“MoviePlace”, movieId{title:"安娜贝尔", img:"URL", date:1999.02.23}
据我正确理解您的问题,您尝试做的是 return toWatch 模型实例,包括 User 和 Movie 模型。 为此,您可以在 findAll() 方法中传递选项,如下所示:
ToWatch.findAll({include: [{model: User}, {model: Movie}]})... //and rest of your code
或者为了保持代码整洁,您可以使用作用域:
// below the toWatch-model.js file
ToWatch.addScope('userIncluded', {include: {model: User}})
ToWatch.addScope('movieIncluded', {include: {model: Movie}})
并且在 getToWatch 方法中:
// you can pass several scopes inside scopes method, its usefull when you want to combine query options
ToWatch.scopes('userIncluded', 'movieIncluded').findAll()... //rest of your code
有关更多信息,请查看 sequelize 文档