NodeJS 无法读取未定义的属性“getTableName”

vsmadaxz  于 2023-04-05  发布在  Node.js
关注(0)|答案(4)|浏览(144)

我遇到了一个问题,当我试图将一个表关联到我的查询时,我得到了一个错误消息Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined。我在表之间有一对一的关系,不确定这是导致错误的原因,还是我在其他地方关联了两个表。
下面是我的疑问:

appRoutes.route('/settings')

    .get(function(req, res, organization){
        models.DiscoverySource.findAll({
            where: { 
                organizationId: req.user.organizationId
            },
            include: [{
                model: models.Organization, through: { attributes: ['organizationName', 'admin', 'discoverySource']}
            }]
        }).then(function(organization, discoverySource){
            res.render('pages/app/settings.hbs',{
                user: req.user,
                organization: organization,
                discoverySource: discoverySource
            });
        })

    })

下面是模型。DiscoverySource模型:

module.exports = function(sequelize, DataTypes) {

var DiscoverySource = sequelize.define('discovery_source', {
    discoverySourceId: {
        type: DataTypes.INTEGER,
        field: 'discovery_source_id',
        autoIncrement: true,
        primaryKey: true
    },
    discoverySource: {
        type: DataTypes.STRING,
        field: 'discovery_source_name'
    },
    organizationId: {
        type: DataTypes.TEXT,
        field: 'organization_id'
    },
},{
    freezeTableName: true,
    classMethods: {
        associate: function(db) {
            DiscoverySource.belongsTo(db.Organization, {foreignKey: 'organization_id'});
        },
    },
});
    return DiscoverySource;
}

这是我的模型。组织模型:

module.exports = function(sequelize, DataTypes) {

var Organization = sequelize.define('organization', {
    organizationId: {
        type: DataTypes.INTEGER,
        field: 'organization_id',
        autoIncrement: true,
        primaryKey: true
    },
    organizationName: {
        type: DataTypes.STRING,
        field: 'organization_name'
    },
    admin: DataTypes.STRING
},{
    freezeTableName: true,
    classMethods: {
        associate: function(db) {
            Organization.belongsToMany(db.User, { through: 'member', foreignKey: 'user_id' });
        },
    }
});
    return Organization;
}
cbwuti44

cbwuti441#

你需要重写你的查询,看起来像:

models.DiscoverySource.findAll({
    attributes: ['discoverySource'],
    where: { 
        organizationId: req.user.organizationId
    },
    include: [{
        model: models.Organization,
        attributes: ['organizationName', 'admin']
    }]
})

根据Sequelize文档:
[options.attributes] -要选择的属性的列表,或具有include和exclude键的对象。
[options.include[].attributes] -要从子模型中选择的属性列表。
[options.include[].through.where] -对belongsToMany关系的连接模型进行过滤。
[options.include[].through.attributes] -从belongsToMany关系的连接模型中选择的属性列表。
因此,[options.include[].through]只能用于Belongs-To-Many关联,而不是您用于DiscoverySourceOrganization模型的Belong-To

ntjbwcob

ntjbwcob2#

在我的案例中

through: { attributes: [] }

解决了问题。

w8f9ii69

w8f9ii693#

我也遇到了同样的问题。我的续作版本5.22.3。如果您使用的是相同的版本,请对您的查询进行以下更改:

models.DiscoverySource.findAll({
    attributes: ['discoverySource'],
    where: { 
        organizationId: req.user.organizationId
    },
    include: {
        model: models.Organization,
        //rest of the code, if any
    }
})

include不再需要是数组。

2ledvvac

2ledvvac4#

如果需要在include中指定所需的列,请修改代码,使其看起来像这样:

models.DiscoverySource.findAll({
            where: { 
                organizationId: req.user.organizationId
            },
            include: [{
                model: models.Organization, 
                attributes: ['organizationName', 'admin', 'discoverySource']
            }]

因此,您将不再需要使用“through”关键字。

相关问题