【问题标题】:How to use Sequelize associations with .then in controller如何在控制器中使用带有 .then 的 Sequelize 关联
【发布时间】:2021-07-03 15:04:16
【问题描述】:

这是我在 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;

我看了很多教程,但是我第一次尝试创建一个方法来返回所有内容,包括通过 ID 从我的其他表中返回的内容,我不确定在哪里放置以及如何在这个方法中放置我需要的数据,考虑到它有.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{ 红色, 电影院:“电影广场”, movieId{title:"Anabel", img:"URL", date:1999.02.23}

【问题讨论】:

    标签: javascript node.js sequelize.js associations


    【解决方案1】:

    正如我正确理解您的问题一样,您尝试做的是返回包含 User 和 Movie 模型的 Watch 模型实例。 为此,您可以在 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
    

    有关更多信息,请查看续集文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2014-11-07
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多