2016-03-21 40 views
0

Bire çok ilişkisi olan iki modeli ilişkilendirmeye çalıştığım bir sorunla karşılaşıyorum. Nedense bu sorgu, ilişkiyi referans göstermesine rağmen bir hata atıyor.SequelizeJS 'getTableName' özelliği okunamıyor

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',{ 
       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; 
} 

Organizasyon:

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.hasMany(db.DiscoverySource, {foreignKey: 'organization_id'}); 
     }, 
    } 
}); 
    return Organization; 
} 
Bu rota

Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined 
    at generateJoinQueries (/Users/user/Desktop/Projects/node/project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1181:43) 

: Burada

benim hata mesajı
+0

Olası kopyası [Sequelize Association Error undefined 'getTableName' özelliği okunamıyor] (http://stackoverflow.com/questions/35974514/sequelize-association-error-cannot-read-property-gettablename-of-undefined) – Milkncookiez

cevap

1

Görünüşe göre, bu bir Sequelize Association Error Cannot read property 'getTableName' of undefined kopyasıdır. Sequelize belgelerine göre

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

:

Ancak, şöyle görünecek şekilde sorguyu yeniden yazmak gerekir

[options.attributes] - İstediğin niteliklerin bir listesi seçin veya dahil et ve hariç tut tuşlarıyla bir nesne.

[options.include []. Öznitelikler] - Çocuk modelinden seçim yapılacak özelliklerin listesi.

[options.include []. Through.where] - belongsToMany ilişkileri için birleşim modeline filtre uygular.

[options.include []. Through.attributes] - belongsToMany ilişkileri için birleştirme modelinden seçim yapılacak niteliklerin listesi. Yani

, [options.include[].through] sadece DiscoverySource ve Organization modelleri için tarafınızca kullanılan Belongs-To-Many dernek ziyade Belong-To durumunda kullanılabilir.

İlgili konular