包括模型SequelizeJS

时间:2014-02-26 10:39:23

标签: node.js sequelize.js

当我尝试使用SequelizeJS在“findAll”动作中包含模型时出现错误...

    Executing (default): SELECT `projects`.*, `Medias`.`id` AS `Medias.id`, `Medias`.`title` AS `Medias.title`, `Medias`.`image` AS `Medias.image`, `Medias`.`createdAt` AS `Medias.createdAt`, `Medias`.`updatedAt` AS `Medias.updatedAt`, `Medias`.`projectId` AS `Medias.projectId` FROM `projects` LEFT OUTER JOIN `media` AS `Medias` ON `projects`.`id` = `Medias`.`projectId` ORDER BY id DESC;

    TypeError: Cannot call method 'hasOwnProperty' of null
        at Function.Dot.transform (/Users/anthonycluse/Sites/Sbaf/Back-End/node_modules/sequelize/node_modules/dottie/dottie.js:102:50)
        at /Users/anthonycluse/Sites/Sbaf/Back-End/node_modules/sequelize/lib/dialects/abstract/query.js:366:17
        at Array.forEach (native)
        at module.exports.groupJoinData (/Users/anthonycluse/Sites/Sbaf/Back-End/node_modules/sequelize/lib/dialects/abstract/query.js:365:10)
        at module.exports.handleSelectQuery (/Users/anthonycluse/Sites/Sbaf/Back-End/node_modules/sequelize/lib/dialects/abstract/query.js:233:17)
        at module.exports.AbstractQuery.formatResults (/Users/anthonycluse/Sites/Sbaf/Back-End/node_modules/sequelize/lib/dialects/abstract/query.js:70:34)
        at null.<anonymous> (/Users/anthonycluse/Sites/Sbaf/Back-End/node_modules/sequelize/lib/dialects/mysql/query.js:33:35)
        at Query.Sequence.end (/Users/anthonycluse/Sites/Sbaf/Back-End/node_modules/mysql/lib/protocol/sequences/Sequence.js:78:24)
        aMacBook-Pro-de-Anthony:Back-End anthonycluse$ 

我的行动:

dbContext.project.findAll({order: 'id DESC', include: [{model: dbContext.media, as: 'Medias'}]}).success(function(projects) {
        callback(projects);
});

我的dbContext:

(function () {

    var modelsPath = __dirname + '/../app/models/';

    /**
    * Constructor.
    * Add your entities 'DbSet' instance here like user.
    */
    function DbContext() {
        this.db = require('./dbConnection');
        this.entities();
        this.modelBuilder();
    }

    /**
     * Attach your model to DbContext like user to perform database sync.
     * 
     */
    DbContext.prototype.entities = function() {
        this.project = this.db.import(modelsPath + 'project');
        this.media = this.db.import(modelsPath + 'media');
    };

    /**
    * Manage Database entities associations here.
    */
    DbContext.prototype.modelBuilder = function () {
        this.project.hasMany(this.media, {as: 'Medias'});
        this.media.belongsTo(this.project);
    };

    module.exports = DbContext;
})();

0 个答案:

没有答案