Node.js 模型函数中的续集访问关系

Node.js 模型函数中的续集访问关系,node.js,sequelize.js,models,eager-loading,Node.js,Sequelize.js,Models,Eager Loading,我的数据库中有两个表“modules”和“composants”。模块可以有许多组件,并且具有int属性“prix”。我想为模块创建一个伪属性“prix”,它将其组件的所有属性添加到“prix”。如何访问创建伪属性的函数中的关联组件 module.exports = function (sequelize, DataTypes) { "use strict"; var module = sequelize.define('module', { id_module:

我的数据库中有两个表“modules”和“composants”。模块可以有许多组件,并且具有int属性“prix”。我想为模块创建一个伪属性“prix”,它将其组件的所有属性添加到“prix”。如何访问创建伪属性的函数中的关联组件

module.exports = function (sequelize, DataTypes) {
    "use strict";
    var module = sequelize.define('module', {
        id_module: {
            type: DataTypes.INTEGER.UNSIGNED,
            autoIncrement: true,
            primaryKey: true
        },
        nom: {
            type: DataTypes.STRING,
            allowNull: false
        },
        coupe: {
            type: DataTypes.STRING,
            allowNull: false
        },
        gamme: {
            type: DataTypes.STRING,
            allowNull: false
        },
        prix: {
            type     : DataTypes.INTEGER.UNSIGNED,
            allowNull: false,
            get      : function(models)  {
                var prix;
                console.log(this);
                return prix;
            }
        }
    }, {
        classMethods: {
            associate: function(models) {
                module.hasMany(models.caracteristique_module,{
                    hooks: true,
                    onDelete: 'cascade',
                    foreignKey: "id_caracteristique"
                });
                module.hasMany(models.composant, {
                    through: models.composition,
                    hooks: true,
                    onDelete: 'cascade',
                    foreignKey: "id_composant"
                })
            }
        },
        timestamps: false,
        tableName: 'modules'
    });
    return module;
};
找到了解决办法。 在模型中

prix: {
            type     : DataTypes.INTEGER.UNSIGNED,
            allowNull: false,
            get      : function()  {
                var prix = 0;
                if(this.composants != undefined){
                    this.composants.forEach(function(composants){
                        prix += parseInt(composants.prix);
                    });
                }
                return prix;
            }
        }